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?

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.