I am trying to generate a table with php for loop, that lists numbers. Something like this:
1 | 2 | 3 | 4 | 5
2 | 3 | 4 | 5 | 1
3 | 4 | 5 | 1 | 2
4 | 5 | 1 |
<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 5; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
echo "<td>", (($col + $row) % 5) + 1, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
echo "<table border='1'><br />";
for ( $i = 0; $i < 5; $i++ ) {
echo "<tr>";
for ( $j = 0; $j < 5; $j++ ) {
echo "<td>", ($j+$i)%5+1, "</td>";
}
echo "</tr>";
}
echo "</table>";
My Version
<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 5; $row ++) {
$k=$row;
for ($col = 0; $col < 5; $col ++) {
echo "<td>", (($k++)%5)+1, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
My version :
<?php
echo "<table border='1'><br />";
$i=1;
for ($row = 0; $row < 5; $row ++) {
echo "<tr>";
$cont = 0;
for ($col = $i; $col <= 5; $col ++)
{
echo "<td>", ($col), "</td>";
$cont++;
}
if($cont < 5)
{
for($col = 1; $col <= 5 - $cont; $col++)
{
echo "<td>", ($col), "</td>";
}
}
echo "</tr>";
$i++;
}
echo "</table>";
Codepad: http://codepad.viper-7.com/JZogNY
<?php
echo '<table border="1">';
$i = 0;
for($i =1; $i<=5; $i++){
echo '<tr>
<td>'.$i.'</td>';
$x = 0;
for($x=1; $x<=4; $x++){
$y = $x + $i;
$z = ($y>5) ? $y-5 : $y;
echo '<td>'.$z.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>