如何使用 PHP 显示文件夹中的所有图像
How to display all images within a folder with PHP
我目前正在使用以下代码 return 显示文件夹中所有图片的页面:
echo''; $array=glob('ebayimg/9150gba041117/size_2/*');
foreach($array as $zf)
echo '<img src="http://www.gbamedical.com/'.$zf.'">’;
我需要能够做类似于下面的代码,除了代码中的每张图片需要出现两次,而且我需要为那里的许多图片动态添加单词 "image1, image2, etc"在文件夹中。 . . .如下所示。下面的示例只显示了两张图片,但我需要它来 return 代码,因为文件夹中有很多图片。
<div class="slider">
<input type="radio" name="slide" id="image1" checked/>
<label for="image1">
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9633.JPG"/>
</label>
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9633.JPG" height="480"/>
<input type="radio" name="slide" id="image2"/>
<label for="image2">
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9638.JPG"/>
</label>
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9638.JPG" height="480"/>
</div>
因为 glob() returns an numeric-based array, use the $key => $value syntax of foreach. That way $index
will correspond to the indexes (0 - the last index). Also, checked 可以有条件地包含在字符串中。请参阅下面的示例。
foreach($array as $index => $zf)
$checked = ''; //not checked
if (!$index) { // equal to 0, for first item
$checked = 'checked';
}
echo '<input type="radio" name="slide" id="image'.($index+1).'" '.$checked.'/>'.
'<label for="image'.($index+1).'">'.
'<img src="http://www.gbamedical.com/'.$zf.'" />'.
'</label>'.
'<img src="http://www.gbamedical.com/'.$zf.'" height="480" />';
}
你可以这样做:
<?php
$array = glob('ebayimg/9150gba041117/size_2/*');
$i = 1;
foreach($array as $image) {
?>
<input type="radio" name="slide" id="image<?php echo $i ?>" <?php if($i == 1){ ?> checked <?php } ?>/>
<label for="image<?php echo $i ?>">
<img src="http://www.gbamedical.com/<?php echo $image ?>"/>
</label>
<img src="http://www.gbamedical.com/<?php echo $image ?>"/>
<?php
$i++;
}
?>
我目前正在使用以下代码 return 显示文件夹中所有图片的页面:
echo''; $array=glob('ebayimg/9150gba041117/size_2/*');
foreach($array as $zf)
echo '<img src="http://www.gbamedical.com/'.$zf.'">’;
我需要能够做类似于下面的代码,除了代码中的每张图片需要出现两次,而且我需要为那里的许多图片动态添加单词 "image1, image2, etc"在文件夹中。 . . .如下所示。下面的示例只显示了两张图片,但我需要它来 return 代码,因为文件夹中有很多图片。
<div class="slider">
<input type="radio" name="slide" id="image1" checked/>
<label for="image1">
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9633.JPG"/>
</label>
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9633.JPG" height="480"/>
<input type="radio" name="slide" id="image2"/>
<label for="image2">
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9638.JPG"/>
</label>
<img src="http://www.gbamedical.com/ebayimg/9150gba041117/size_2/IMG_9638.JPG" height="480"/>
</div>
因为 glob() returns an numeric-based array, use the $key => $value syntax of foreach. That way $index
will correspond to the indexes (0 - the last index). Also, checked 可以有条件地包含在字符串中。请参阅下面的示例。
foreach($array as $index => $zf)
$checked = ''; //not checked
if (!$index) { // equal to 0, for first item
$checked = 'checked';
}
echo '<input type="radio" name="slide" id="image'.($index+1).'" '.$checked.'/>'.
'<label for="image'.($index+1).'">'.
'<img src="http://www.gbamedical.com/'.$zf.'" />'.
'</label>'.
'<img src="http://www.gbamedical.com/'.$zf.'" height="480" />';
}
你可以这样做:
<?php
$array = glob('ebayimg/9150gba041117/size_2/*');
$i = 1;
foreach($array as $image) {
?>
<input type="radio" name="slide" id="image<?php echo $i ?>" <?php if($i == 1){ ?> checked <?php } ?>/>
<label for="image<?php echo $i ?>">
<img src="http://www.gbamedical.com/<?php echo $image ?>"/>
</label>
<img src="http://www.gbamedical.com/<?php echo $image ?>"/>
<?php
$i++;
}
?>