dongxie2613 2013-04-09 23:25
浏览 106
已采纳

创建三个div,每个div包含1/3的内容

I am trying to create a mock up of a three-column Pinterest layout that scans a directory of images and puts 1/3 of the images in each of the columns.

But I can't figure out how to get the correct images to show up in the column.

It's not important that all the images are included, I just want the columns to be look moderately even.

Here's what I have so far, but obviously this just lists all the images in each of the columns.

<?php
    $dir    = 'img';
    $files = scandir($dir);
    $count = round((count($files)/3), 0);
?>

<div class="column">
    <?php 
        foreach ($files as $file) { // 1/3 of the images
            if ($file != "." && $file != "..") {
                    echo "<img src=\"img/" . $file . "\">" . "
";
            }
        }
    ?>
</div>

<div class="column">
    <?php 
        foreach ($files as $file) { // 1/3 of the images
            if ($file != "." && $file != "..") {
                    echo "<img src=\"img/" . $file . "\">" . "
";
            }
        }
    ?>
</div>

<div class="column">
    <?php 
        foreach ($files as $file) { // 1/3 of the images
            if ($file != "." && $file != "..") {
                    echo "<img src=\"img/" . $file . "\">" . "
";
            }
        }
    ?>
</div>



Update: Ultimately I used this

<?php
    $dir    = 'img';
    $files = scandir($dir);
    $count = round((count($files)/3), 0);
?>

<div class="column">
<?php 
    // 1/3 of the images
    for ($i = 0; $i < floor(count($files) / 3); $i++) {
        $file = $files[$i];
        if ($file != "." && $file != "..") {
                echo "<img src=\"img/" . $file . "\">" . "
";
        }
    }
?>
</div>

<div class="column">
    <?php
        // 2/3 of the images
        for ($i = floor(count($files) / 3); $i < floor(count($files) / 3) * 2; $i++) {
            $file = $files[$i];
            if ($file != "." && $file != "..") {
                    echo "<img src=\"img/" . $file . "\">" . "
";
            }
        }
    ?>
</div>

<div class="column">
    <?php 
        // 3/3 of the images
        for ($i = floor(count($files) / 3) * 2; $i < count($files); $i++) {
            $file = $files[$i];
            if ($file != "." && $file != "..") {
                    echo "<img src=\"img/" . $file . "\">" . "
";
            }
        }
    ?>
</div>
  • 写回答

2条回答 默认 最新

  • dongyao5843 2013-04-09 23:32
    关注

    I would use for loops, like this:

    // 1/3 of the images
    for ($i = 0; $i < floor(count($files) / 3); $i++) {
        $file = $files[$i];
    
    // 2/3 of the images
    for ($i = floor(count($files) / 3); $i < floor(count($files) / 3) * 2; $i++) {
        $file = $files[$i];
    
    // 3/3 of the images
    for ($i = floor(count($files) / 3) * 2; $i < count($files); $i++) {
        $file = $files[$i];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用