Jump to content

[SOLVED] Run code every 3 times in a while loop


BrandonE97

Recommended Posts

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?

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++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.