-----Original Message----- From: Steve Keller To: [EMAIL PROTECTED] At 1/3/2003 12:25 AM, Lightfirst wrote:
Can someone explain to me why the loop that counts to 99 appears before the 5 by 5 grid in the following php code? Yes. ## else { ## echo "<td align=\"center\" valign=\"middle\" width=\"15%\" height=\"77\" border=\"1\" bordercolor=\"#000000\">"; ## echo "<div align=\"center\"><font size=\"1\"></font></div>"; ## echo "Hello" ; $i++; ## } //for else You start the <TD> tags, but whenever your conditional echoes "Hello," you're not echoing the </TD>. And when you come out of your loop, you never close your <TABLE> tag. I hope you also realize that you're opening and closing that <DIV> and <FONT> without putting the "Hello" inside of it. Here's a good tip someone gave me when I first started learning PHP: when you're dealing with HTML, it's a good idea to use \n at the end of your echoed lines and \t's at the beginnings to create staggered indentations, this makes it a little easier to read when you're testing the output. -------------------------- When you have large blocks of (almost) pure HTML like this, I usually reckon it's a better bet still to jump out of PHP mode completely, and just write your HTML inline -- then it's *much* easier to see if it's malformed! A couple of other observations: * In "if ($c==0 || $c%7==0)", the first test is completely redundant, since 0 % anything == 0 * Most of your <td> tag is the same in all three branches, so I'd strip that out and echo it before the first "if". * Speaking purely personally here, but whenever I see PHP code full of comments explaining what all its closing }s are for, I wonder why on earth the author doesn't just get into using the :-style syntax for block structures. So my take on this would go something like: <table border="1" align="center" width="100%" bgcolor="#FFFFFF" bordercolor="#FFFFFF"> <tr> <?php for ($r=0; $r<5; $r++): for ($c=0; $c<7; $c++): ?> <td align="center" valign="middle" height="77" bordercolor="#000000" width=<?php if ($c%7==0): ?>"15%"></td> <?php elseif ($c%6==0): ?>"14%"></td> </tr> <tr> <?php else: ?>"15%" border="1"> <div align="center"><font size="1"> Hello <?php echo $i++ ?></font></div> </td> <?php endif; // ($c) endfor; // ($c) endfor; // ($r) ?> </tr> </table> <?php for ($i=1; $i<100; $i++): echo "$i<br>\n"; endfor; ?> Yes, it is a slightly different way of thinking to code like this, and it may not suit you -- but I find it more convenient for pages that have a largish percentage of actual HTML on them. (And if you have an editor that can do syntax highlighting in different ranges of colours for HTML and PHP, it makes it really easy to pick one out from the other, and especially to follow their separate logic structures.) Cheers! Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php