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

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

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.

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

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.

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

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

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

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

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.

Related

PHP In while loop how to get one static item after each 10 items? [duplicate]

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
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.
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';
}
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.
every 3 posts?
if($counter % 3 == 0){
echo IMAGE;
}
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;
}
How about: if(($counter % $display) == 0)
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 '+'; }
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.

PHP loop over every second element of array [duplicate]

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
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.
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';
}
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.
every 3 posts?
if($counter % 3 == 0){
echo IMAGE;
}
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;
}
How about: if(($counter % $display) == 0)
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 '+'; }
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.

For loop and if statement every few times

I have for loop from 1 to 6000 and I would like to execute a function every 100 times. I have only such an idea:
for($i=0;$i<=6000;$i++)
{
if($i==100 && $i==200 ... $i==6000) do something;
}
How can I solve this problem differently?
From http://php.net/manual/en/language.operators.arithmetic.php
if ($i % 100 == 0) {
// $i can be fully divided by 100
}
The modulo operator (%) tells you if a number divided by another number has a remainder. If the remainder is 0, you know the first number is a multiple of the second (since it divides evenly).
Just check if i is a multiple of 100:
for($i=0;$i<=6000;$i++)
{
if($i % 100 == 0) {
}
}
I agree with the answers this question has received already. However, you might want to omit the case when $i is 0. So you can check it in your for loop if you are starting from 0.
for($i=0; $i<=6000; $i++)
{
if($i != 0 && $i % 100 == 0){
// do something
}
}

PHP Explode function. If function

Could someone help suggest in below php explode function, we are displaying script after 5th listing. How is it possible to display script exactly after 5th listing and 10th listing on a page which has more than 10 listings
We tried using
if ($i == 5 & $i== 10)
but it does not work
Below is original code - which displays script after 5th listing
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 0; $i < $numberOfListings; ++$i)
{
if ($i == 5)
{ ?>
<script> </script>
<?php }
echo $listings[$i] . "<hr/>";
}
?>
Edit
How is it like - if have to display a separate script on $i==9, could you advise.
Because $i starts at 0 (0 to 9 is 10, whilst 0 to 10 is 11). Try if ($i == 4 || $i== 9), with an or operator.
Also I would not use the && (the and operator), because it is unlikely $i will ever equal both 4 and 9. I'd suggest you read into Truth Tables (and maybe Propositional Calculus) because from seeing what you had tried originally, it would be helpful to understand how a truth table works.
(source: wlc.edu)
You can use the contine, continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
$arr = range(0,9);
foreach($arr as $number) {
if($number < 5) {
continue;
}
print $number;
}
Ref: http://php.net/manual/en/control-structures.continue.php
Try using modulus operator
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
for($i = 1; $i < $numberOfListings; ++$i)
{
if ($i%5 == 0)
{
echo "in";
?>
<script> </script>
<?php
}
echo $listings[$i-1] . "<hr/>";
}
Here we are looping from 1 and there for $i <= $numberOfListings
and while listing we will use $listings[$i-1]
DEMO CODE AT http://codepad.viper-7.com/lrTOgP

Different css styling for last element of a php array

When looping through an array how can I create a different css div style for the last element to be output in my array.
for($i=0;$i<=count($productid);$i++){if($productrank[$i]>0 ){
<? if (($productid[$i] % 2 ) && !( last element of array){ echo '<div class="centerBoxContentsFeatured centeredContent back vLine " style="width:50%;">';}
else { echo '<div class="centerBoxContentsFeatured centeredContent back " style="width:50%;">';} ?>
Just check if it is the last $productid
for(...)
{
if ($i === (count ($productid) - 1))
// Last one -> special CSS
}
}
Also, DO NOT use count() in a FOR loop if you don't really have to.
Just assign a value BEFORE and use it :
$count_temp = count ($productid);
for ($i = 0; $i < $count_temp; ++$i)
And use this $count_temp again if the IF statement to check if it's the last element
Answer to comment :
How would this same method get the first element?
if ($i === 0)
Or
// Special CSS for $i = 0
// Start loop at 1 instead of 0
for ($i = 1; $i < $count_temp; ++$i)
You can do this purely with CSS using :last-child. It is supported by all modern browsers.
div.centerBoxContentsFeatured:last-child {
/* special styles for last */
}
See it in action: http://jsfiddle.net/9y93j/1/
Like this:
if($productid[$i] == $productid[count($productid)-1]){
//last element code here
} elseif (($productid[$i] % 2 ){
//even row code here
} else {
//odd row code here
}

Categories