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

PhpHtmlLoops

Php Problem Overview


I wanted to echo an image every after 3 post via XML here is my code :

<?php
// URL of the XML feed.
$feed = 'test.xml';
// How many items do we want to display?
//$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
  die('The XML file could not be found!');
}
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
  echo ' 
  <div style="float:left; width: 180px; margin-top:20px; margin-bottom:10px;">
 image file</a> <div class="design-sample-txt">'. $post->author.'</div></div>
';

  // Increase the counter by one.
  $counter++;
  // Check to display all the items we want to.
  if($counter >= 3) {
  	echo 'image file';
	}
  //if($counter == $display) {
    // Yes. End the loop.
   // break;
  //}
  // No. Continue.
}
?>

here is a sample first 3 are correct but now it doesn't loop idgc.ca/web-design-samples-testing.php

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

Going off of @Powerlord's answer,

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

You can still start your counter at 0 (arrays, querys), but offset it

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

Solution 3 - Php

Use the modulo arithmetic operation found here in the PHP manual.

e.g.

$x = 3;

for($i=0; $i<10; $i++)
{
    if($i % $x == 0)
    {
        // display image
    }
}

For a more detailed understanding of modulus calculations, click here.

Solution 4 - Php

every 3 posts?

if($counter % 3 == 0){
    echo IMAGE;
}

Solution 5 - Php

You can also do it without modulus. Just reset your counter when it matches.

if($counter == 2) { // matches every 3 iterations
   echo 'image-file';
   $counter = 0; 
}

Solution 6 - Php

How about: if(($counter % $display) == 0)

Solution 7 - Php

I am using this a status update to show a "+" character every 1000 iterations, and it seems to be working good.

if ($ucounter % 1000 == 0) { echo '+'; }

Solution 8 - Php

It will not work for first position so better solution is :

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

Check it by yourself. I have tested it for adding class for every 4th element.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkwek-kwekView Question on Stackoverflow
Solution 1 - PhpPowerlordView Answer on Stackoverflow
Solution 2 - PhpHatrixView Answer on Stackoverflow
Solution 3 - PhpGreg BView Answer on Stackoverflow
Solution 4 - PhpmateuszaView Answer on Stackoverflow
Solution 5 - PhpJulezView Answer on Stackoverflow
Solution 6 - PhpIvarView Answer on Stackoverflow
Solution 7 - PhpmemeView Answer on Stackoverflow
Solution 8 - PhpMohanView Answer on Stackoverflow