在 HTML 表格中每 n 个单元格创建一个新行

问题描述 投票:0回答:5

我有以下代码:

for ($i = 0; $i < count($gallery); $i++)
{
    $temp = array();
    $temp = $gallery[$i];
    echo "<img src='". $temp->path . "' />";
}

现在这段代码将内容打印在一行中。我只想每行打印 3 个,然后创建新行并打印另外 3 个,依此类推。这怎么办?

编辑:错误

遇到 PHP 错误

严重性:通知

消息:未定义的偏移量:5

文件名:views/profile.php

行号:105

遇到 PHP 错误

严重性:通知

消息:尝试获取非对象的属性

文件名:views/profile.php

行号:106

php html arrays html-table presentation
5个回答
5
投票

你可以做

$n = 3;
echo "<table><tr>";
for($i=0; $i<count($gallery);$i++){
    $temp = array();
    $temp = $gallery[$i];
    echo "<td><img src='". $temp->path . "' /></td>";
    if($i % $n ==0 && $i!=0 ){
        echo "</tr><tr>";
    }
}
echo '</tr></table>';

编辑:

如果您想以“正确”的方式做到这一点 - 通过构建语法正确的 HTML,您需要这样做:

$n = 3;
echo "<table><tr>"; 
$gallery_count = count($gallery);
for($i=0; $i<$gallery_count; $i++){
    $temp = array();
    $temp = $gallery[$i];
    echo "<td><img src='". $temp->path . "' /></td>";

    if($i != 0){
        if($i % $n == 0 && $i != $gallery_count-1){
            echo "</tr><tr>";
        }
        else{
            echo ""; //if it is the last in the loop - do not echo
        }
    }
}

//example - if the last 2 `td`s are  missing:
$padding_tds  = $gallery_count % $n;
if($padding_tds != 0 ){
    $k = 0;
    while($k < $padding_tds){
       echo "<td>&nbsp;</td>";
    }
}
echo '</tr></table>';

0
投票

您只需要使用

modulus
来检查已打印的数量 - 3 的任何倍数都会添加一个中断。

$x=0;
for($i=0; $i<count($gallery);$i++)
{
    $x++;       
    $temp = array();
    $temp = $gallery[$i];
    echo "<img src='". $temp->path . "' />";
    if (($x%3)==0) { echo "<br />"; }

}

0
投票

我只是用表格重新设计了它,它更加整洁,因为每件事都会被格式化以看起来正确。 这有点混乱,因为我只是添加了一点 if 语句来释放表。

<table>
<?php
$number_per_row = 3;
for($i=0; $i<count($gallery);$i++)
{
    $temp = array();
    $temp = $gallery[$i];
    if(($i % $number_per_row) == 0) {
        echo "<tr>";
    }
    ?>
<td><?php echo "<img src='". $temp->path . "' />"; ?></td>
<?php
    if(($i % $number_per_row) == $number_per_row - 1) {
        echo "</tr>";
    }
}
?>
</table>

0
投票

使用

array_chunk()
建立 3 行的子集将使该过程更易于阅读并避免模数条件错误。

为了防止表格行不平衡,当没有要显示的图像路径时,请在剩余单元格中默认为空值。

代码:(演示

$gallery = [
    (object) ['path' => 'imgs/img_a.jpg'],
    (object) ['path' => 'imgs/img_b.png'],
    (object) ['path' => 'imgs/img_c.jpeg'],
    (object) ['path' => 'imgs/img_d.gif'],
];

$template = <<<HTML

    <tr>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
    <tr>
HTML;
echo '<table border="1">';
    foreach (array_chunk($gallery, 3) as $set) {
        printf(
            $template,
            "<img src=\"{$set[0]->path}\">",
            isset($set[1]) ? "<img src=\"{$set[1]->path}\">" : '',
            isset($set[2]) ? "<img src=\"{$set[2]->path}\">" : ''
        );
    }
echo "\n</table>";

-1
投票
$n = 3;
echo "<table>";
echo "<tr>";
for($i=0; $i<count($gallery);$i++){
    if(($i % n ==0) && ($i != 0)){
        echo "</tr><tr>";
    }
    $temp = array();
    $temp = $gallery[$i];
    echo "<td><img src='". $temp->path . "' /></td>";
}
echo "</tr>";
echo '</table>';``
© www.soinside.com 2019 - 2024. All rights reserved.