duanlv2788 2011-09-15 14:29
浏览 41
已采纳

在PHP中每6个返回循环后输出文件夹并清除浮动

I'm trying to count the images in my folder and want to clear the float after each 6 images returned from a folder. Here's the code I've got so far but it spits out the clear more than the amount I'd like for it. Could somebody maybe guide me to a solution? Thanks in advance. Below is my code.

<?php
$i = 0;
$dirname = "images"; 
$images = scandir($dirname);
$filecount = count(glob("images/". "*.png"));
 //echo $filecount;

$ignore = Array(".", "..", "otherfiletoignore");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {

if ($i % 6 === 0){

echo "<div style='clear:both;></div>'";

}

echo "<div style='float:left;'><img src='images/",$curimg."'"," /></div>";

}
}

?>
  • 写回答

2条回答 默认 最新

  • dryift6733 2011-09-15 14:42
    关注

    You're not ever changing the value of $i, therefore it will be zero every time through the loop. In this situation you want to use a for loop instead of a foreach loop.

    $images = glob("images/*.png");
    $filecount = count($images);
    $ignore = Array(".", "..", "otherfiletoignore");
    for ($i = 0; $i < $filecount; $i++){
        if(!in_array($images[$i], $ignore)) {
            if ($i % 6 === 0){
                    echo "<div style='clear:both;></div>'";
            }
            echo "<div style='float:left;'><img src='images/",$images[$i]."'"," /></div>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错