1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multiple looping

Discussion in 'PHP' started by Grendor, Apr 20, 2005.

  1. #1
    Is it possible to shorten this Multiplication table using multiple loops, perhaps foreach and for:

    <html>
    <head>
    <title>Multiplication table 1-10</title>
    </head>
    <body>
    <?
    $broj=array(1,2,3,4,5,6,7,8,9,10);
    echo "<table cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"blue\" border=\"1\" align=\"center\">";
    foreach ($broj as $niz)
    {
    if ($niz % 2==0) {$color="red";
    }
    else {
    $color="blue";
    }
    {
    echo "<tr><td>";
    echo $niz . "*1=";
    echo "</td><td>";
    echo $niz*1;
    echo "</td><td>";
    echo $niz . "*2=";
    echo "</td><td>";
    echo $niz*2;
    echo "</td><td>";
    echo $niz . "*3=";
    echo "</td><td>";
    echo $niz*3;
    echo "</td><td>";
    echo $niz . "*4=";
    echo "</td><td>";
    echo $niz*4;
    echo "</td><td>";
    echo $niz . "*5=";
    echo "</td><td>";
    echo $niz*5;
    echo "</td><td>";
    echo $niz . "*6=";
    echo "</td><td>";
    echo $niz*6;
    echo "</td><td>";
    echo $niz . "*7=";
    echo "</td><td>";
    echo $niz*7;
    echo "</td><td>";
    echo $niz . "*8=";
    echo "</td><td>";
    echo $niz*8;
    echo "</td><td>";
    echo $niz . "*9=";
    echo "</td><td>";
    echo $niz*9;
    echo "</td><td>";
    echo $niz . "*10=";
    echo "</td><td>";
    echo $niz*10;
    echo "</td></tr>";
    }
    }
    echo "</table>";
    echo "<span style=\"color: $color;\">";
    echo "</span><br />\n";
    ?>
    </body>
    </html>
     
    Grendor, Apr 20, 2005 IP
  2. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #2
    
    <table>
    
    <?
    	for($i=1;$i<=12;$i++)
    	{
    		printf("<tr>");
    		for($j=1;$j<=12;$j++)
    		{
    			printf("<td align=right>%d",$i*$j);
    		}
    		printf("\n");
    	}
    ?>
    </table>
    
    
    PHP:
    That will create a multiplication table.
     
    palespyder, Apr 20, 2005 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ...just be sure to use your closing tags...
    
    <table>
    
    <?
        for($i=1;$i<=12;$i++)
        {
            printf("<tr>");
            for($j=1;$j<=12;$j++)
            {
                echo '<td align=right>'.($i*$j).'</td>';
            }
            printf("</tr>\n");
        }
    ?>
    </table>
    PHP:
     
    exam, Apr 20, 2005 IP
  4. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #4
    Thanks EXAM! I been staring at code all day so I can see how I missed that ;)
     
    palespyder, Apr 20, 2005 IP