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.

 

 

Here's the script:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  7. <title>Nested Loop Multiplication Table</title>
  8. </head>
  9.  
  10. <body>
  11. <h2>Nested Loop Multiplication Table</h2>
  12.  
  13. <?php
  14. //Generate an HTML table
  15. echo "<table border=\"1\">";
  16.  
  17. //Generate table data showing the numbers 1-7 multiplied by each other, 
  18. //starting with the rows. 
  19. for ($row=1; $row<=7; $row++){
  20.   echo "<tr>\n";
  21.   //Generate each entry in the row to create the columns.
  22.   for ($col=1; $col<=7; $col++){
  23.     //First, do the math.
  24.     $x=$col * $row;
  25.     //Then send the value to the table with the table data tags.   
  26.     echo "<td>$x</td>\n";
  27.   }
  28.   echo "</tr>";
  29. }
  30. echo "</table>";
  31. ?>
  32.  
  33. </body>
  34. </html>

See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.

To open a PHP code editor in a new tab, click here.

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.