BrandonE97 Posted July 20, 2007 Share Posted July 20, 2007 I need a certain block of code to run inside a while loop every 3 times its run but I cant figure out how to write it. I came up with a sort of static fix but I need it to be more expandable. Here is what I've got inside the loop so far. if ($num == 3 || $num == 6 || $num == 9 || $num == 12 || $num == 15 || $num == 18 || $num == 21 || $num == 24 ect...) { echo('</tr><tr>'); } The loop output 3 profiles with images and names on each line and then goes to a new line for the next 3 using tables. Is there a way to write this so it runs every 3 on to infinity and not have to input the numbers manualy? Quote Link to comment Share on other sites More sharing options...
jorgep Posted July 20, 2007 Share Posted July 20, 2007 Ok, I imagine two ways to do it: The first one: with a counter var counter = 0; if(counter == 3){ echo('</tr><tr>'); counter = 0; } else { counter++; } The other way could be using an non-reseted counter counter=0; while(your condition here){ if(counter%3 == 0){ echo('</tr><tr>'); } counter++; } Quote Link to comment Share on other sites More sharing options...
BrandonE97 Posted July 20, 2007 Author Share Posted July 20, 2007 Thanks bunches. I can kick myself for not figuring that one out lol. Quote Link to comment Share on other sites More sharing options...
jorgep Posted July 20, 2007 Share Posted July 20, 2007 Hehe thats ok, sometimes that happens good luck Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 20, 2007 Share Posted July 20, 2007 if you want to know an alternative, simplified method... <?php while(...) { if ( ($c++)%3 == 0) { echo "run: " . $c . "<br />\r\n"; } }?> Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 21, 2007 Share Posted July 21, 2007 sorry, code fart in that last post (why cant i edit my own posts?!) it should be ++$c, not $c++ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.