sys_debug
sys_debug

Reputation: 4003

Create a new row every n cells in an HTML table

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?

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

Upvotes: 0

Views: 3230

Answers (5)

mickmackusa
mickmackusa

Reputation: 47992

Using array_chunk() to establish subsets of 3 rows will make the process easier to read and avoid modulus condition mistakes.

To prevent making imbalanced table rows, default an empty value in remaining cells when there is no image path to present.

Code: (Demo)

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

Upvotes: 0

karthikr
karthikr

Reputation: 99660

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

Upvotes: 5

Justin
Justin

Reputation: 43

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>

Upvotes: 0

Biswajit Maji
Biswajit Maji

Reputation: 889

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

Upvotes: -1

Drew
Drew

Reputation: 472

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

}

Upvotes: 0

Related Questions