Modulo

December 19, 2006

This one is easy, but so important but used so rarely that you have time to forget it. How to build a table programmatically with a certain number of columns, with only one while - loop:

<table style="border: 1px solid black">

$i = 0;
$col = 4;
$val = 20;
$table = null;

while ($i < $val)
{
    $i++;
    $table .= '<tr>' + ($i % $col == 0) ? "<td>$i</td></tr>" : "<td>$i</td>";
}
print ($table);
?>

</table>