Page 1 of 1

help with for loops

Posted: Tue Feb 26, 2008 9:09 am
by gammaman
How can I change this code

Code: Select all

 
<html>
<table border="1">  
<tr>
    <th></th>
<?php
    for($i=1; $i<=9;$i++){
        echo "<th>", $i, "</th>";  //print the column header
    }
?>
</tr>
<?php
    for ($i=1; $i<=9; $i++){
        echo "<tr>";
        echo "<td><i><b>", $i, "</b></i>";  //print the row header
        for ($j=1; $j<=9;$j++){
            echo "<td>";  $k=$j*$i; echo $k;    echo "</td>\n";
        }
        echo "</tr>\n";
    }
?>
</table>
</html>
 
To print the following (See attachment)

Re: help with for loops

Posted: Tue Feb 26, 2008 11:40 am
by Christopher

Code: Select all

            echo "<td>";
             if ($j <= $i) {
                 echo $j * $i;
             } else {
                 echo '&nbsp;';
             }
             echo "</td>\n";