For Loop Table in PHP

前端 未结 5 917
终归单人心
终归单人心 2021-01-04 08:02

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 |         


        
相关标签:
5条回答
  • 2021-01-04 08:20
    <?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>";
    ?>
    
    0 讨论(0)
  • 2021-01-04 08:29
    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>";
    
    0 讨论(0)
  • 2021-01-04 08:34

    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>";
    ?>
    
    0 讨论(0)
  • 2021-01-04 08:36

    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

    0 讨论(0)
  • 2021-01-04 08:42
    <?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>';
    ?>
    
    0 讨论(0)
提交回复
热议问题