<html>
<body>
<h1>A Multiplication Table</h1>
<?php
//----- Very, very simple nested loop ---------------------------------
print "<h3>Very simple nested 'for' loops</h3>";
for ($row =1; $row <= 10; $row++) //loop through 10 rows
{
for ($col =1; $col <= 10; $col++) //loop through 10 columns
{
print $row * $col; //row times column
print " ";
}
print "<br />";
}
print "<br />";
//----- Changing to while loop, and placing output in html table ---------------------
print "<h3>Changing to a 'while' loop, and adding HTML table</h3>";
print "<table border='1'> \n";
for ($row =1; $row <= 10; $row++) //loop through 10 rows
{
print "<tr>";
$col = 1;
while ($col <= 10) //loop through 10 columns
{
print "<td width='25' align='right'>";
print $row * $col; //row times column
print "</td>";
$col++;
}
print "</tr> \n";
}
print "</table> \n";
//----- Adding row and column headings ---------------------------------
print "<h3>Adding row and column headings </h3>";
print "<table border='1'> \n";
print "<tr bgcolor='cyan'>";
print "<th width='25'> X </th>";
for ($colHead =1; $colHead <= 10; $colHead++) //loop 10 times
print "<th width='25'>" . $colHead . "</th>"; //print column headers
print "</tr> \n";
for ($row =1; $row <= 10; $row++)
{
print "<tr>";
print "<th bgcolor='cyan'> $row </th>"; //print row header
$col = 1;
while ($col <= 10)
{
print "<td align='right'>" . $row*$col . "</td>";
$col++;
}
print "</tr> \n";
}
print "</table> \n";
//----- Reprinting with row and column headings (yet another way) ----------------------------
print "<h3>Adding row and column headings - yet another way </h3>";
print "<table border='1'> \n";
for ($row =0; $row <= 10; $row++) //loop through 11 rows
{
print "<tr>";
$col = 0;
while ($col <= 10) //loop through 11 columns
{
if ($row == 0 && $col == 0)
print "<th bgcolor='tan'> X </th>";
if ($row == 0 && $col > 0)
print "<th bgcolor='tan' width='25'> $col </th>";
if ($row > 0 && $col == 0)
print "<th bgcolor='tan' width='25'> $row </th>";
if ($row > 0 && $col > 0)
print "<td align='right'>" . $row*$col . "</td>";
$col++;
}
print "</tr> \n";
}
print "</table> \n";
?>
<?php include "../include.php"; ?> <!-- hyperlink to see the code -->
</body>
</html>