dpfqy5976 2013-09-03 04:35 采纳率: 100%
浏览 86
已采纳

使用php循环html div来显示文件夹中的图像

I have a folder "blah" that contains a lots of images.

I want to show all my images into my page.

Here my html code:

<div class="cp-thumb cp-masonry cp-thumbs616 masonry-brick">
   <img src="blah/225cd24c144611e3b69022000a1deb4b_7.jpg" class="img_235x235" />
</div>

When I use php for loop to show all the image, it does not work:

<?php
for($i=0; $i<=10; $i++)
{
   echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
       <img src='blah/225cd24c144611e3b69022000a1deb4b_7.jpg'class='img_235x235' />
     </div>";      
}     
?>

Anyone can help? Thanks

  • 写回答

7条回答 默认 最新

  • dsoxcj7276 2013-09-03 04:39
    关注

    Try with GLOB like

    foreach(glob('blah/'.'*') as $filename){
        echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
                  <img src='".$filename."' class='img_235x235' />
              </div>"; 
    }
    

    COnsidered that all the image names are different and also the directory contains only images.

    In my local I have images in my proj/blah/ So I have done like

    <img src='proj/".$filename."' class='img_235x235' />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?