如何在完成特定数字后自动分割单元格并开始新行 [英] How to autocut cells after a specific number have been made and start a new row

查看:108
本文介绍了如何在完成特定数字后自动分割单元格并开始新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含超过100张图片的json文件

I have a json file with an array of over 100 images

foreach($images as $image)
    {
                  $imagelink = $image['link'];
                  echo "<tr>";
                  echo "<td><img src=$imagelink></td>";
                  echo "</tr>";
    }

问题是,此代码每行打印1个图像,另一个

The problem is, this code prints out 1 image per line and the other one is below it.

我怎么能像图片显示在一行4

How can I do it like the images are shown 4 in a line

像结构一样这:

Structure like this:

IMAGE   IMAGE   IMAGE   IMAGE
IMAGE   IMAGE   IMAGE   IMAGE

而非

instead of

IMAGE
IMAGE
IMAGE

请帮忙!
Thanks!

Please help! Thanks!

推荐答案



How about this:

echo "<table>";

$col = 0;
$maxCols = 4;
foreach($images as $image)
{
    // first row, or we've already output the max number of images per row so start a new one
    if( $col % $maxCols == 0 ) {
        // we need to end a previously started row
        if( $col != 0 ) {
            echo "</tr>";
        }

        echo "<tr>";
    }

    $imagelink = $image['link'];
    echo "<td><img src=$imagelink></td>";

    $col++;
}

// we didn't end the last row we started
if( $col != 0 ) echo "</tr>";

echo "</table>";

这篇关于如何在完成特定数字后自动分割单元格并开始新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆