0

My table has rows that's looped in a non-specific length because the values in the cells may be added or removed anytime. Anyway, here's the code:

<?php 
    $i = 1;
    foreach($items as $item => $itemValue) { 
    if ($itemValue['item_id'] == $parentItemValue['id']) {
        if (fmod($i,7)) echo '<tr>';
        echo '<td class="inner-td"><input type="checkbox" id="itemId">'.$itemValue['item'].'</td>';
        if (!fmod($i,7)) echo '</tr>';
        $i++;
    }
?>

The above code displays this: enter image description here

I also tried if (!fmod($i,7)) echo '<tr>' and if (!fmod($i,8)) echo '</tr>' and gives me this: enter image description here

Also, if (!fmod($i,10)) echo '<tr>' and if (!fmod($i,11)) echo '</tr>' and gives me this: enter image description here

I want my table to look like this: enter image description here

Is there a way that the cells will fill in the entire row before making a new one?

Ethelene Laverne
  • 279
  • 2
  • 6
  • 21

2 Answers2

1

You can try this. Just change the $maxcol value for how many columns you want.

<?php
$tmp = array('test1','test2','test3','test4');
echo '<table border="1">';
$x = 0;
$maxcol = 2; // Max column
foreach($tmp as $i=>$v)
{

    echo $x === 0 ? '<tr>' : '';
    echo '<td>'.$v.'</td>';
    echo $x === ($maxcol-1) ? '</tr>' : '';
    $x++;
    $x = $x == $maxcol ? 0 : $x;

}
echo '</table>';
?>
rmondesilva
  • 1,732
  • 3
  • 17
  • 29
0

Try to follow this structure. Note that all fields and rows are made up by myself.

 <tbody>

        <?php 
        require_once ('connectionWithDB.php');
        $table = "members"; 
        $array = $admin_query->viewTableData($table);
        foreach ($array as $row){
        print_r($array); 
        ?>

        <tr>
         <td> <?php $row[member_id]  ?> </td>
         <td> <?php $row[member_password]  ?> </td>
         <td> <?php $row[member_first_name]  ?> </td>
         <td> <?php $row[member_last_name] ?> </td>
         <td> <?php $row[member_DOB]  ?> </td>
         <td> <?php $row[member_address]  ?> </td>
         <td> <?php $row[member_email]  ?> </td>
         <td> <?php $row[member_phone]  ?> </td>
         <td> <?php $row[member_gender]  ?> </td>
        </tr>
       </tbody>
Achilles
  • 411
  • 1
  • 5
  • 27