-2

A couple of days ago, I made a post similar to this one.

I continued my work, but went back to this exercise after.

I need 2 more patterns in a table. The last 2 tables, I did with nested For-loops, which worked excellent, but now I'm stuck again.

What I need:

                  1
               2 1
            3 2 1
         4 3 2 1
      5 4 3 2 1
   6 5 4 3 2 1

What I tried:

<table>
        <b>Patroon III</b>
        <?php
            $rows = 6;
            for($row = 1; $row <= $rows; $row++){
                echo "<tr>";
                for($col = 6; $col >= $row; $col--){
                    if($col <= $row){ 
                        echo "<td class='td2'>" . $col . "</td>";
                    }
                    else {
                        echo "<td class='td2'>" . " " . "</td>";
                    }
                }
                echo "</tr>";
            }
        ?>
</table>

What I get:

                  1
               2
            3
         4
      5
   6

And the second table I need:

1 2 3 4 5 6
   2 3 4 5 6
      3 4 5 6
         4 5 6
            5 6
               6

What I tried:

<table>
    <b>Patroon IV</b>
    <?php
        $rows = 6;
        for($row = 1; $row <= $rows; $row++){
            echo "<tr>";
            for($col = 1; $col <= $row; $col++){
                if($col >= $row){ 
                    echo "<td class='td2'>" . $col . "</td>";
                }
                else {
                    echo "<td class='td2'>" . " " . "</td>";
                }
            }
            echo "</tr>";
        }
    ?>
</table>

What I get:

1
   2
      3
         4
            5
               6

Any suggestions? I'm thinking about another nested for-loop, but I don't know where to start. Not sure if the if-else statement is needed.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Emielh
  • 9
  • 1

1 Answers1

0

This is the very basic thing in php. You just need to think about the logic. That's it.

Here is your solution to your question

<b>Patroon III</b>
<table border=1>

        <?php
            $rows = 6;
            for($row = 1; $row <= $rows; $row++){
                echo "<tr>";
                for($col = 6; $col >= 1; $col--){
                    if($col <= $row){ 
                        echo "<td class='td2'>" . $col . "</td>";
                    }
                    else {
                        echo "<td class='td2'>" . " " . "</td>";
                    }
                }
                echo "</tr>";
            }
        ?>
</table>
<b>Patroon IV</b>
<table border=1>
    <?php
        $rows = 6;
        for($row = 1; $row <= $rows; $row++){
            echo "<tr>";
            for($col = 1; $col <= $rows; $col++){
                if($col >= $row){ 
                    echo "<td class='td2'>" . $col . "</td>";
                } else {
                    echo "<td class='td2'>" . " " . "</td>";
                }
            }
            echo "</tr>";
        }
    ?>
</table>
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47