PHP: How to Determine Every Nth Iteration of a Loop

PHP: How do you determine every Nth iteration of a loop?

The easiest way is to use the modulus division operator.

if ($counter % 3 == 0) {
echo 'image file';
}

How this works:
Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.

There is one catch: 0 % 3 is equal to 0. This could result in unexpected results if your counter starts at 0.

PHP Get every Nth item in loop but group them by sets of 6

You just need to check if the counter is equal to 0 to 5 modulo 6; move the $counter++ to the end of the loop, and replace each comparison with a compare to 1 less e.g.

<?php if ($counter == 3) { ?>

becomes

<?php if (($counter % 6) == 2) { ?>

So your loop as a whole would be:

    <?php foreach ($events as $event) { ?>
<div class="eventsGroup">

<?php if (($counter % 6) == 0) { ?>
<div>
<div class="gridblock">
<div class="gridblock_inner">
<?php echo $event['name']; ?>
</div>
</div>
<?php } ?>

<?php if (($counter % 6) == 1) { ?>
<div class="gridblock sixty">
<div class="gridblock_inner no_right_margin">
<?php echo $event['name']; ?>
</div>
</div>
</div>
<?php } ?>

<?php if (($counter % 6) == 2) { ?>
<div style="width:66.666667%;float:left;">
<div class="gridblock onehundred">
<div class="gridblock_inner">
<?php echo $event['name']; ?>
</div>
</div>
<?php } ?>

<?php if (($counter % 6) == 3) { ?>
<div class="gridblock fifty">
<div class="gridblock_inner">
<?php echo $event['name']; ?>
</div>
</div>
<?php } ?>

<?php if (($counter % 6) == 4) { ?>
<div class="gridblock fifty">
<div class="gridblock_inner">
<?php echo $event['name']; ?>
</div>
</div>
</div>
<?php } ?>

<?php if (($counter % 6) == 5) { ?>
<div style="width:33.333333%;float:left;">

<div class="gridblock onehundred twohigh">
<div class="gridblock_inner no_right_margin doublehigh">
<?php echo $event['name']; ?>
</div>
</div>

</div>
<?php } ?>
</div>
<? $counter++; ?>

<?php } ?>

PHP nth iteration

if ( $i % 8 == 0) {
echo '</div><!--.container--><div class="container">';
}

Mod operator returns the remainder. You can simply state $i % 8 or $i % 8 == 0 or $i % 8 === 0 as john stated

But you will need to also open the next div in your conditional depending on how you structure this. An example:

<div class="outer-container">
<div class="inner-container">
<?php
$images; // object
$i = 1;

while ( $images->have_images() ) {

echo 'content here';
//print_r($i % 8);
if ( $i % 8 == 0 && $images->total > 8) {
echo '</div><!--.inner-container--><div class="inner-container">';
}

$i++;
}
?>
</div>
</div>

Added conditional in case your images total happens to be 8 it won't create a rogue empty div.

I'm trying to understand your comment. Are you saying you have an outer container, then an inner container, and inside this inner container you want the loop you defined in the question where each group of 8 is contained in a container div?

I added a print r in the loop you can uncomment to verify $i is doing what you want it to. By this code and the fact that $i begins at 1 you should not experience the closing div conditional prematurely.

PHP (Laravel) loop - every n-th entry

You could declare a counter variable before the foreach statement, and then increment it after each iteration.
You just test if counter % 4 equals 0. If it does, you insert your quote.

Loop through array to assign value for every nth item

The modulo operator (%) is the answer

$range = range(1, 100);
$rangeValues = array();

for ($i = 0; $i < count($range); $i++){
// using modulo 25 returns values from 0-24, but you want 1-25 so I use ($i % 24) +1 instead which gives 1-24
$rangeValues[$range[$i]] = ($i % 24) +1;
}

Foreach, special treatment of every n:th item (odd, even for example)

Use the modulus operator. I see @Alex has beaten me to the mark with this, but I offer this code I wrote and tested, so that others can see more clearly the principle:

$blogusers=array('a','b','c','d','e','f','g','h','i','j');
$i=0;
foreach ($blogusers as $bloguser) {
if($i % 4 === 0) $extraclass= "fourthClass";
$resultHTML .= "<div class=\"standardClass $extraclass\">$bloguser</div>";
$i++;
$extraclass="";
}
echo $resultHTML;

Could be made more compact with the ternary operator, but this is the principle.

Loop echo something every 4th time in php

Yes, you can use a modulo. It's good for nth time with simple arithmetic. When you were a child in grade school, you did modulo math: it's the remainder in the quotient of a division problem. When the remainder is zero, then you have evenly divided.

Zero modulo an integer will yield zero. So, the outer iteration loop below begins at one so that you can skip over a common base case of $x=0.

for ($x = 1; $x <11; $x++){
// inside a given loop of ten iterations . . .

// determine if this is a plain case or the 4th time
// if it is the fourth time, load a 1 in $type; otherwise, load a 0.
$type = ((($x%4)==0)?1:0);
// prepare to respond with hello or goodbye based on $type
$output = (($type==0)?"hello":"goodbye");
// echo $output once or twice based on $type
for($i=0, $j=1+$type; $i < $j ;$i++){
echo "<p>$x $output</p>";

}
}

Will generate output like:

1 hello

2 hello

3 hello

4 goodbye

4 goodbye

5 hello

6 hello

7 hello

8 goodbye

8 goodbye

9 hello

10 hello

HTML every nth iteration with a twist--the nth changes every xth time using array values

I think the issue you're having is in the if($i++%...) section of your code.

Instead of incrementing $i and checking the result of the modular expression, just check if $i == $footnote[$m] and then reset $i back to 0 when it is a success.

I modified your script a little locally, try this out:

$i = $m = 0;

$bridge .= '<div class="grid block menu">';

foreach($value['sections'] as $section)
{
if ($i == $footnote[$m])
{
$bridge .= '</div><div class="grid block menu">';
$m++;
$i = 0;
}
$secname = $section['name'];
$dishcount = count($section['items']);

$bridge .= '<h3>'. $secname .' '.$footnote[$m].'</h3>';

$i++;
}

$bridge .= '</div>';

This way, you are actually iterating through each footnote instead of just checking to see if it is divisible by the number specified.



Related Topics



Leave a reply



Submit