18

I'm currently checking whether an entry in a loop is the third iteration or not, with the following code:

<?php for ($i = 0; $i < count($category_news); $i++) : ?>

    <div class="grid_8">
        <div class="candidate snippet <?php if ($i % 3 == 2) echo "end"; ?>">
            <div class="image shadow_50">
                <img src="<?php echo base_url();?>media/uploads/news/<?php echo  $category_news[$i]['url']; ?>" alt="Image Preview" width="70px" height="70px"/>
            </div>
               <h5><?php echo $category_news[$i]['title']?></h5>
            <p><?php echo strip_tags(word_limiter($category_news[$i]['article'], 15)); ?></p>
            <?php echo anchor('/news/article/id/'.$category_news[$i]['news_id'], '&gt;&gt;', array('class' => 'forward')); ?>
        </div>
    </div>

    <?php if ($i % 3 == 2) : ?>
         </li><li class="row">
    <?php endif; ?>

<?php endfor; ?>

How can I check if the loop is in its second and not its third iteration?

I have tried $i % 2 == 1 to no avail.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Udders
  • 6,914
  • 24
  • 102
  • 194

7 Answers7

24

Modulus checks what's the leftover of a division.

  • If $i is 10, 10/2 = 5 with no leftover, so $i modulus 2 would be 0.
  • If $i is 10, 10/3 = 3 with a leftover of 1, so $i modulus 3 would be 1.

To make it easier for you to track the number of item I would start $i from 1 instead of 0. e.g.

for($i=1; $i <= $count; $i++)
    if($i % 2 == 0) echo 'This number is even as it is divisible by 2 with no leftovers! Horray!';
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
8

When in doubt, write a snippet of code:

for ($j = 1; $j < 4; $j++)
{
   for ($k = 0; $k < $j; $k++)
   {
      echo "\n\$i % $j == $k: \n";

      for ($i = 0; $i < 10; $i++)
      {
         echo "$i : ";
         if ($i % $j == $k)
         {
            echo "TRUE";
         }
         echo " \n";
      }
   }
}

Here is the output. Use it to figure out what you need to use:

$i % 1 == 0: 
0 : TRUE 
1 : TRUE 
2 : TRUE 
3 : TRUE 
4 : TRUE 
5 : TRUE 
6 : TRUE 
7 : TRUE 
8 : TRUE 
9 : TRUE 

$i % 2 == 0: 
0 : TRUE 
1 :  
2 : TRUE 
3 :  
4 : TRUE 
5 :  
6 : TRUE 
7 :  
8 : TRUE 
9 :  

$i % 2 == 1: 
0 :  
1 : TRUE 
2 :  
3 : TRUE 
4 :  
5 : TRUE 
6 :  
7 : TRUE 
8 :  
9 : TRUE 

$i % 3 == 0: 
0 : TRUE 
1 :  
2 :  
3 : TRUE 
4 :  
5 :  
6 : TRUE 
7 :  
8 :  
9 : TRUE 

$i % 3 == 1: 
0 :  
1 : TRUE 
2 :  
3 :  
4 : TRUE 
5 :  
6 :  
7 : TRUE 
8 :  
9 :  

$i % 3 == 2: 
0 :  
1 :  
2 : TRUE 
3 :  
4 :  
5 : TRUE 
6 :  
7 :  
8 : TRUE 
9 :  
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
  • 1
    This is a really useful snippet! Idiots guide - increasing the '4' in the first line will output more loops with bigger gaps before repeating. – phil Sep 15 '15 at 09:27
  • Yes, that is what some of the other answerers ought to have done. We need comprehensive answers, not half-baked one-liners. – Peter Mortensen Apr 03 '20 at 21:49
7

Now for the answer:

How can I check if the loop is in its second and not its third iteration?

$i % 2 === 0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
1

To answer the question:

How can I check if the loop is in its second AND not its third iteration?

When you want to do two things in a conditional, we use the logical operator &&. To cite the official documentation...

Example: $a && $b

Name: And

Result: TRUE if both $a and $b are TRUE.

So, for code, you'll want...

if($i % 2 === 0 && $i % 3 !== 0) {
     // Loop is in its second and not its third iteration!
}

We use a modulus to solve this, which returns the remainder of the division for dividing by 2 and 2.

division parts named

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
1

For every third iteration you need:

if ($i % 3 === 0)

If a particular third iteration, then:

if ($i === 3)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gfox
  • 307
  • 1
  • 6
0

I think it should be:

if ($i % 2 == 0) 
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

Try this. It should work for every third iteration:

if ($i % 3 === 0) 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Riz
  • 9,703
  • 8
  • 38
  • 54