PHP: Finish and start a new <tr> after 6 lines

I want to create a table, and then for each 6 rows there should be tr, and then the rows are inside td.

So an example:

<tr>
<td><?php echo $show[id]; ?></td> // 1
<td><?php echo $show[id]; ?></td> // 2
<td><?php echo $show[id]; ?></td> // 3
<td><?php echo $show[id]; ?></td> // 4
<td><?php echo $show[id]; ?></td> // 5
<td><?php echo $show[id]; ?></td> // 6
</tr> <tr> // start new tr after 6 rows
...repeat the tds

How can I do something like this? I tried to do myself

<tr> 
<?php
while ($show == mysql_fetch_array($query)){ ?>
<td><?php echo $show[id]; ?></td>
<?php } ?>
</tr>

But, as you can see, this just inserts everything into one tr ..

thank

+3
source share
5 answers
<tr> 
<?php
$c = 0; // Our counter
$n = 6; // Each Nth iteration would be a new table row
while ($show = mysql_fetch_array($query))
{
  if($c % $n == 0 && $c != 0) // If $c is divisible by $n...
  {
    // New table row
    echo '</tr><tr>';
  }
  $c++;
  ?>
  <td><?php echo $show[id]; ?></td>
  <?php
} ?>
</tr>

Related links:

+18
source

Count the number of lines if modulo 6 is null, then an echo </tr><tr>

<tr> 
<?php
$i=0;
while ($show == mysql_fetch_array($query)){ ?>
    <td><?php echo $show[id]; ?></td>
    <?php if(++$i%6 == 0) echo '</tr><tr>'; ?>
<?php } ?>
</tr>
+4
source

:

<tr>
<?php
$count = 0;
while ($show = mysql_fetch_array($query)){ 
        if($count == 6) {
                $count = 0;
                echo "</tr> <tr>";
        }
        echo "<td>".$show[id]."</td>";
        $count++;
}
</tr>
+3

== =.

, Id :

$perRow = 6;
$counter = 0;
echo '<tr>';
while ($show = mysql_fetch_array($query)) {
    if ($counter % $perRow === 0 && $counter !== 0) {
        echo '</tr><tr>';
    }
    echo '<td>', $show['id'], '</td>';
    $counter++;
}
while ($counter++ % $perRow !== 0) {
    echo '<td></td>';
}
echo '</tr>';

, .

+3
<?php 
$countRows = 0;
while ($show == mysql_fetch_array($query)){ 
if($countRows == 0) echo '<tr>';
?>
<td><?php echo $show[id]; ?></td>
<?php 
$countRows++;
if($countRows == 6){
 $countRows = 0;
 echo '</tr>';
}
?>
<?php } ?>
<?php if($countRows < 6) echo '</tr>'; ?>
+2

Source: https://habr.com/ru/post/1763448/


All Articles