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? Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/ 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++; } Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/#findComment-303741 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. Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/#findComment-303744 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 Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/#findComment-303745 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"; } }?> Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/#findComment-303782 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++ Link to comment https://forums.phpfreaks.com/topic/61038-solved-run-code-every-3-times-in-a-while-loop/#findComment-303791 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.