Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Control Structures Ex. #4: Nested For Loops

HTML tables involve a lot of repetitive coding - a perfect place to use for loops. You can do even more if you nest the for loops.

In this PHP exercise, use two for loops, one nested inside another. Create the following multiplication table:

1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49


Unless you want to try your hand at CSS styling, don't be concerned about the appearance of the table, its borders and text alignment. The point of the exercise it to create the data and the table tags using the for loops.

 

Comments

I suggest a slightly modified solution, I entered the data directly as the product of the row and column number:

<?php   
	echo "<table style=\"border-collapse: collapse; border: 1px solid black;background-color: #ffff00\">";
	
	for ($i = 1; $i<=7; $i++)
	{
		echo "<tr>";
		for ($j = 1; $j <=7; $j++) 
		{
			echo "<td style=\"width: 2em; text-align:center;border: 1px solid black;\">" . $i * $j . "</td>";	
		}
		echo "</tr>";	
	}
	
	echo "</table>";
?>

PS: I also styled the table to make it like in the example.

hi.
I used your line:
echo "";
but dont know why it didnt work for me so i thought to exempt the table from the script & it worked (sorry, i am just about a wk old in programming as a whole- working against a deadline)..
So, is this acceptable? Thx!

  1. <body>
  2. <table border= ''3'' cell spacing ''10'' cell padding ''1''>
  3. <tr>
  4. <td> 1 </td>
  5. <td> 2 </td>
  6. <td> 3 </td>
  7. <td> 4 </td>
  8. <td> 5 </td>
  9. <td> 6 </td>
  10. <td> 7 </td>
  11. </tr>
  12. <?php
  13. for ($rws=1; $rws<=7; $rws++) {
  14. echo ''<tr>\n'';
  15. for ($cols=1; $cols<=7;$cols++) {
  16. $result=$rws*$cols;
  17. echo ''<td>$result</td>\n'';
  18. }
  19. echo ''</tr>'';
  20. }
  21. ?>
  22. </table>
  23. </body>

  1. echo "<table border=\"1\">";
  2.  
  3. 	 for($foo = 1; $foo <= 7; $foo++) {
  4. 	 echo "<tr>\n";
  5.              for($bar = $foo ; $bar <= 7 * $foo; $bar += $foo) {
  6.                  echo "<td>$bar</td>\n";
  7.  
  8.              }
  9.              echo "<br />";
  10.              echo "</tr>";
  11.          }
  12.  echo "</table>";

  1. for($x=1;$x<=10;$x++)
  2. {
  3.  
  4. 	for($y=1;$y<=10;$y++)
  5. 	{
  6. 		echo $x*$y;
  7. 		echo "|";
  8. 	}
  9. 	echo "<br/>";
  10. 	echo "---------------------------";
  11. 	echo "<br/>";
  12. }

the simple code i came up with:

  1. echo '<table class="mytable">';
  2.   for ($counter1=1;$counter1<=7;$counter1++) {
  3.   echo "<tr>";
  4.     for ($counter2=1;$counter2<=7;$counter2++)
  5.       echo "<td>".$counter1*$counter2."</td>";			
  6.   echo "</tr>";
  7.   }
  8. echo '</table>';

then i place this code within the "head" section of html doc to style it a bit:

  1. <style>
  2.   .mytable {
  3.   border-spacing: 10px;
  4.   border: 2px solid; }
  5. </style> 

add additional styling to spice it up.