help with for loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

help with for loops

Post 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)
Attachments
table.png
table.png (49.92 KiB) Viewed 333 times
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help with for loops

Post by Christopher »

Code: Select all

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