Home > Enterprise >  Foreach table output rows near each other
Foreach table output rows near each other

Time:06-03

I have json html table in php, now i can't get it work that the output in the table will be near each other.

I am already few days busy with this, hope some one can help me.

  <?php  foreach($data2 as $row): ?>
    <tr>
        <td><?=$row['model'];?></td>
        <td><?=$row['model2'];?></td>
    </tr>
<?php endforeach;?>

enter image description here

My json output:

enter image description here

CodePudding user response:

It looks like the JSON is not formatted for what you are trying to do. Every object has either modal or modal2, so the one that is not filled is undefined.

You should get the length of the longest list and use a normal for loop.

<?php ($index = 0; $index <= $lengthOfLongestList; $index  ): ?>
    <tr>
        <td><?=$data2[index]['model'];?></td>
        <td><?=$data2[index]['model2'];?></td>
    </tr>
<?php endforeach;?>
  • Related