12

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 | 2 | 3
5 | 1 | 2 | 3 | 4

I still have problems getting it, this is actually quite simple, but I have not been able to solve it. So far I have the following code:

<?php
echo "<table border='1'><br />";

for ($row = 0; $row < 5; $row ++) {
   echo "<tr>";

   for ($col = 1; $col <= 4; $col ++) {
        echo "<td>", ($col + ($row * 4)), "</td>";
   }

   echo "</tr>";
}

echo "</table>";
?>

However, this only generates the following:

1  | 2  | 3  | 4 
5  | 6  | 7  | 8
9  | 10 | 11 | 12
13 | 14 | 15 | 16
17 | 18 | 19 | 20

Thank you, any help would be appreciated!

3
  • 2
    This should give you an idea $a = array(1,2,3,4,5); array_unshift($a, array_pop($a)); Commented Apr 1, 2014 at 13:37
  • 1
    This is called "queue" in data structures. Read this phpmoot.com/other-sorting-options Commented Apr 1, 2014 at 13:47
  • Not a queue... smells like modular arithmetic to me. Commented Apr 1, 2014 at 19:28

5 Answers 5

18
<?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>";
?>
Sign up to request clarification or add additional context in comments.

Comments

6
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>";

Comments

3

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

Comments

2

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>";
?>

Comments

0
<?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>';
?>

1 Comment

consider adding the explanation rather than adding the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.