[英]Display images from directory?
我有一個代碼來顯示文件夾中的圖像,這很好用,但是在Google chrome中它顯示了一個額外的框,例如image。我如何解決這個問題?
這是我的代碼:
<?php
$files = glob("photogallery/thumb/group1/*.jpg*");
for ($i=0; $i<=count($files); $i++)
{
$num = $files[$i];
echo '<a href="photo_view.php?gn=1" style="text-decoration:none;"><img src="'.$num.'" width="100px" height="100px" alt=""></a>'." ";
}
?>
問題在於count()
將返回零索引數組中的項目總數。 這意味着如果$files
數組中包含10個項目,則最低索引為0,最高索引為9。
您對$i<=count($files)
意味着在此示例中, $i
從0到10(共11個項目)。
我還是建議使用foreach
。 這是一種遍歷集合的更通用的方法,並且允許稀疏數組。
foreach ($files as $file) {
// use the $file variable here in your echo
}
<?php
$files = glob("photogallery/thumb/group1/*.jpg*");
for ($i=0; $i<=count($files); $i++)
{
if(trim($files[$i])){
$num = $files[$i];
echo '<a href="photo_view.php?gn=1" style="text-decoration:none;"><img src="'.$num.'" width="100px" height="100px" alt=""></a>'." ";
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.