I want to make a table where it changes row after 5 columns. continues to 50. Now $i <= 4 for the row. example:
1 | 2 | 5 | 4 | 5 |
6 | 7 | 8 | 9 | 10|
<table width="300" border="1">
<tbody>
<tr>
<?php
$i = 0;
$x = 0;
do {
echo "<tr>";
$i++;
echo "<td> " . $i . "</td";
}
while ( $i <= 4 );
/* New Row */
echo "</td></tr>";
/* CONTINUE HERE */
?>
</tr>
</tbody>
</table>
use a loop that goes to 50, and check whether the index is a multiple of 5 with if ($i % 5 == 0)
to start a new row.
for ($i = 0; $i < $num_items; $i++) {
if ($i % 5 == 0) { // start a row every 5 items
echo "<tr>";
}
echo "<td>" . ($i + 1) . "</td>";
if ($i % 5 == 4) { // end a row 4 items later
echo "</tr>";
}
}
if ($i % 5 != 4) { // end the last row if there weren't a multiple of 5 items
echo "</tr>";
}
The simplest solution is to insert [tr] when the $i displayed 5 times.
<table width="300" border="1">
<tbody>
<?php
$i=1;
while ($i <=50)
{
if (($i-1) %5==0)
{echo "<tr>";}
echo "<td>". $i . "</td>";
$i++;
}
?>
</tbody>
</table>
This is my solution. variable i % 5 == 0 will start a new row.
<table width="300" border="1">
<tbody>
<tr>
<?php
$i = 0;
$x = 50;
do {
$i++;
echo "<td> " . $i . "</td>";
if($i % 5 == 0)
{
echo "</tr>";
}
}
while ( $i < $x );
?>
</tr>
</tbody>
</table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.