使用glob()从一个目录显示图像,同时回显唯一的第一个图像 [英] Using glob() to display images from a directory while echo'ing a unique first image

查看:156
本文介绍了使用glob()从一个目录显示图像,同时回显唯一的第一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP很新,我不想在这里提出任何问题,直到我无法找到办法。

I am quite new to PHP and I did not want to ask anything here until I absolutely could not find a way to do this.

我有一种情况,我想使用PHP从目录中读取和显示图像,但我希望第一个图像被读取唯一

I have a situation where I would like to use PHP to read and display images from a directory but I would like the first image read to be echo'd uniquely

我当前的代码如下: / p>

My current code as follows:

$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
foreach ($images as $image){
echo '<div class="item">'."\r\n\t\t";
echo '<img src="'.$image.'" alt=""></div>'."\r\n\t";}

只需一个简单的目录拉和回显与格式化。

Just a simple directory pull and echo with formatting.

这将是一个BootStrap轮播,不幸的是,行中的第一个图像必须有< div class =item active> 或整个事情失败。

This will be for a BootStrap carousel and unfortunately the first image in line has to have <div class="item active"> or the whole thing fails.

所以要重申,第一张图片需要是< div class =项目活动> 而其余需要是< div class =item>

So to reiterate, the first image pulled needs to be <div class="item active"> while the rest need to be <div class="item">

我想我在这个周围经历了一大堆香烟,包围我的大脑。

I think I went through an entire pack of cigarettes wracking my brain around this.

推荐答案

为了那个原因。希望能够帮助( - :

use a flag for that. hope to help.(-:

$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
$flag=1;
foreach ($images as $image){
echo '<div class="item' .($flag?' active':''). '">'.PHP_EOL."\t\t";
echo '<img src="'.$image.'" alt=""></div>'.PHP_EOL."\t";
$flag=0;
}

这篇关于使用glob()从一个目录显示图像,同时回显唯一的第一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆