-1

Pretty new to all this, but here i go...

I need to make 2 patterns using a table (without borders) and for-loops:

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

I did work this one out with (probably not the easiest way tho, but it works):

<table>
        <b>Patroon I</b>
        <?php
            $pattern = array();
            for($pyramid = 1; $pyramid <=6; $pyramid++){
                $pattern[$pyramid] = $pyramid;
                echo "<tr>
                        <td class='td1'>" . $pattern[1] . "</td>";
                        if(array_key_exists(2, $pattern)){
                            echo "<td class='td1'>" . $pattern[2] . "</td>";
                        }
                        if(array_key_exists(3, $pattern)){
                            echo "<td class='td1'>" . $pattern[3] . "</td>";
                        }
                        if(array_key_exists(4, $pattern)){
                            echo "<td class='td1'>" . $pattern[4] . "</td>";
                        }
                        if(array_key_exists(5, $pattern)){
                            echo "<td class='td1'>" . $pattern[5] . "</td>";
                        }
                        if(array_key_exists(6, $pattern)){
                            echo "<td class='td1'>" . $pattern[6] . "</td>";
                        }
                echo "</tr>";
            }
        ?>
</table>

and the other pattern

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

Which I can't seem to figure out.
I tried to turn the previous code around, tried rsort($pattern), tried a lot of IF-statements, and now im stuck :S

Anyone has a hint for me?

Thanks!

Emielh
  • 9
  • 1
  • 1
    Is this a school assignment? – Peter Abolins Sep 13 '17 at 12:22
  • yep, it is, first year – Emielh Sep 13 '17 at 12:22
  • You want **just numbers** in the table or `$pattern` is actually an array of something and want to list its content in a table like that? – Marco Sep 13 '17 at 12:23
  • well, i think it would be better to use the array in a forloop instead of just numbers. If i would do just numbers, i probably wouldnt even use echo xD – Emielh Sep 13 '17 at 12:26
  • My question was: "Do you want a table of just numbers like the example you posted?" – Marco Sep 13 '17 at 12:30
  • a table yes, I just made the border 0px – Emielh Sep 13 '17 at 12:31
  • Then what's wrong with nested loops? – Marco Sep 13 '17 at 12:32
  • @Emielh Please fix your question title to make it easier to know what this is about. – brunoais Sep 13 '17 at 12:35
  • The array approach you have employed is really messy, and doesn't scale well. What do you do if the task changes so that the common number on each row is the last one, rather than the first one? You want a solution that can handle changes. Either of the two solutions already offered should do the trick, but before you submit this as your own work, I would suggest that you try to figure out how to make changes, so that you know what the code is doing. – Peter Abolins Sep 13 '17 at 12:42

3 Answers3

3

You don't need any array or fancy function, just two nested for loops.

<table>
        <?php
            // 1
            // 1 2
            // 1 2 3
            // ...
            $rows = 6;
            for ($row = 1; $row <= $rows; $row++) {
                echo "<tr>";
                for ($col = 1; $col <= $row; $col++) {
                    echo "<td class='td1'>" . $col . "</td>";
                }
                echo "</tr>";
            }
        ?>
</table>

<br />

<table>
        <?php
            // 1 2 3 4 5 6
            // 1 2 3 4 5
            // 1 2 3 4
            // ...
            $rows = 6;
            for ($row = $rows; $row > 0; $row--) {
                echo "<tr>";
                for ($col = 1; $col <= $row; $col++) {
                    echo "<td class='td1'>" . $col . "</td>";
                }
                echo "</tr>";
            }
        ?>
</table>

Add the following code before the closing row tag for balanced table rows.

if ($span = $rows - $row)
    echo  "<td colspan='$span'></td>";
Progrock
  • 7,373
  • 1
  • 19
  • 25
Marco
  • 2,007
  • 17
  • 28
1

Try to 2 loops for getting output. Dont use array

  <b>Patroon I</b>
  <br>
  <?php

      for($pyramid = 1; $pyramid <=6; $pyramid++){
          for($i=1;$i<=$pyramid;$i++)
          {
            echo $i." ";
          }
          echo "<br>";
      }
  ?>
  <b>Patroon II</b>
  <br>
  <?php

      for($pyramid = 6; $pyramid >=1; $pyramid--){
          for($i=1;$i<=$pyramid;$i++)
          {
            echo $i." ";
          }
          echo "<br>";
      }
  ?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0
<?php
$pattern=array(1,2,3,4,5,6);
//var_dump($pattern);
 for($pyramid1 = 6; $pyramid1 >=0; $pyramid1--){
     echo "<tr>
            <td class='td1'>";
          for($i = 0; $i <=$pyramid1; $i++){
    echo  $pattern[$i] ;
          }
          echo "</td>";
     echo "</tr>";
       } 
      ?>
       </table>
pedram shabani
  • 1,654
  • 2
  • 20
  • 30