Go To Next Row When Html Table Is Full
Solution 1:
Untested but something like this should work or get you started in a good direction:
<?php$result = mysqli_query($con,"SELECT * FROM table");
echo'<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo"<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?><tdwidth="10%"><?phpecho$row['Name']; ?></td><?php$x++;
}
echo"</tr></table>";
mysqli_close($con);
?>
Solution 2:
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>
. If the remainder is 0 then add a </tr>
Then after the loop a </tr>
if the remainder is not evenly divisible by 10.
<?phpecho'<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?><?phpif ($i%10 ==1): ?><tr><?phpendif; ?><tdwidth="10%"><?phpecho$row['Name']; ?></td><?phpif ($i%10 ==0): ?></tr><?phpendif; ?><?php
}
if ($i%10 != 0) echo"</tr>";
echo"</tr></table>";
Solution 3:
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php$result = mysqli_query($con,"SELECT * FROM table");
echo'<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?></tr><tr><?php
}
?><tdwidth="10%"><?phpecho$row['Name']; ?></td><?php
}
echo"</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1
seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++
before $cell
will cause the first row to be 9 cells instead of 10. Putting $cell > 0 &&
in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if
to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while
loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell
to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php$result = mysqli_query($con,"SELECT * FROM table");
echo'<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?></tr><tr><?php
}
?><tdwidth="10%"><?phpecho$row['Name']; ?></td><?php
}
echo"</tr></table>";
mysqli_close($con);
?>
Solution 4:
<table><tr><?php$endRow = 0;
$columns = 10; // number of columns$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo"<tr>";
?><td><?phpecho$row['Name']; ?></td><?php$endRow++; if($endRow >= $columns) { ?></tr><?php$endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?></table>
This should work fine. Hope this helps.
Post a Comment for "Go To Next Row When Html Table Is Full"