creating new row every n cells in HTML table

泪湿孤枕 提交于 2021-02-16 15:28:11

问题


I have the following code:

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

}

Now this code prints the content in one row. I want to print only 3 per row and then create new row and print another 3 and so on. How can this be done?

appreciate the support :)

EDIT: error

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 5

Filename: views/profile.php

Line Number: 105

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/profile.php

Line Number: 106


回答1:


You can do

$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>';

Edit:

If you want to do it the "right" way - by building the syntactically correct HTML, you need to do:

$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>';



回答2:


You just need to a modulus that checks how many have been printed - any multiple of 3 will add a break.

$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 />"; }

}



回答3:


I just redid it with tables, it's much neater, because each thing will be formatted to look correctly. It's kind of messy because I just added a little if statement to release the tables.

<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>



回答4:


$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>';``


来源:https://stackoverflow.com/questions/16069692/creating-new-row-every-n-cells-in-html-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!