When I say 'Every 3rd Iteration' I mean 'Every 3rd Iteration... starting from number 4'
If it were just every 3rd, I would target the appropriate iterations like so:
<?php if ($count % 3 == 0) : ?>
Bear in mind I've set $count to 1 beforehand
But the iterations need to start with the 4th, then 7th, 10th etc.
Does anyone know how I can acheive this? Can I do it with a simple if statement like the one above?
Unfortunately I don't think a for loop will be possible as this is a WordPress loop I'm dealing with
Your thoughts about using the modulus operator are sound, you just need to expand the scope of your logic surrounding it:
You want every third iteration after the initial 4 have passed. Begin your logic after the first 4:
if ($count > 4)
Then, you want each 3 after that. Your counter includes the initial 4 iterations you didn't want to include, so remove that from your counter and check if the current iteration is a multiple of 3:
if (($count - 4) % 3 === 0)
This should give you what you're looking for.
if ($count > 4)
{
if (($count - 4) % 3 === 0)
{
...
}
}
You can also put this into one line:
if ($count > 4 && ($count - 4) % 3 === 0)
{
...
}
foreach($array as $key=>$value){
if($key % 3 != 0){
continue;
}
}
Related
Within a for loop, i need to add some HTML that outputs only when the loop is on a [(multiple of 3) minus 1].
For example, what i could do is:
for($i=0; $i<count($imagearray); $i++)
{
if($i=="0" || $i=="2" || $i=="5" || $i=="8" || $i=="11")
{
echo 'string';
}
}
but this isnt very elegant and extremely useless for big for loops, is there a proper way to do this?
if ( $i==0 || ($i+1)%3 == 0 )
{
//do stuff
}
What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3
Use the modulus operator.
if (! (($i+1) % 3) ) {
If $i+1 divides into 3 with no remainder, the result will be zero. Then you just need a boolean not.
If you want to match 0 as well (since you use it in your example, but it doesn't match your description) then you will have to special case it with an ||.
You want to use the modulo for that:
(1 % 3) == 1
(2 % 3) == 2
(3 % 3) == 0
(4 % 3) == 1
Good luck
Modulo is the same thing as saying, give me the remainder of a division. So 1 / 3 equals 0 remainder 1, and so on.
if(($i+1)%3 == 0){
//do something
}
The % operator is known as the modulus operator and returns the remainder of a division.
the most elegent method is thus
if ($i % 3 === 2) {
//do stuff
}
as it doesn't add things to the $i value, but all answers are essentially correct!
I am adding in feed Ads to my website. I have a foreach statement that creates a list of posts. I have created a counter that is supposed to count every four posts and insert the Ad content then repeat.
I have tried some other iterations of this but this is the one I can actually get to do something. I can find a lot of info on this exact thing pertaining to wordpress. But I am running cake php and would prefer a pure php solution.
<?php
$count = 1;
foreach($stories as $story) {
echo '<h2>'.$story->title.'</h2>';
if(!empty($story->excerpt)) {
echo $story->excerpt;
} else {
echo limit_text($story->body);
}
if ($count % 4 == 1) {
echo AD_SENSE_INFEED;
}
}
$count++;
?>
This code is what I currently have but its not working the way I would like it to. As if now it basically goes every other. So POST, AD, POST AD...etc.
Your problem isn't a coding problem, its a math problem. What you're using is called modulos or remainders basically.
So that said:
if ($count % 4 == 1) {
For it to equal 1 we have to feed in something that goes in evenly once and leaves one more.
What you want to do is:
if ($count % 4 == 0) {
Aka it means there's no remainder, 4 goes into it evenly with nothing left over.
As #RiggsFolly mentioned and I completely missed this(Give his comment a up vote) your $count variable should be incremented inside the loop as well otherwise it will only increment once after the loop ends.
You can get rid of count all together (and just use the numeric index of the array)
//just some "test" data
$stories = array_fill(0, 100, []);
foreach( $stories as $count => $story) {
echo $count." ".($count % 4)."\n";
if ($count % 4 == 3) {
echo "--------------------------------------\n";
}
}
Output:
0 0
1 1
2 2
3 3
--------------------------------------
4 0
5 1
6 2
7 3
--------------------------------------
...
Sandbox
If your not sure if the keys are in proper order, you can reset them:
foreach(array_values($stories) as $count => $story) {
Obviously an array starts at 0, so you have to offset the % result a bit ... lol ... Yes I am to lazy to increment.
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
}
}
I am trying to achieve a fairly complex layout. I've managed to do it but cant help think that the IF statement is a little lacking in refinement.
I have a for loop which loops through grid items, the first item and every 5th one are larger and every second large item is floated to the right rather than the left. There are four small items to every large one in a row (so the large is the same size as the four small).
I just think my IF isnt particularly elegant and also restricts the size of the grid.
$i = 0;
foreach ($items as $item) {
if ($i % 5 == 0) { ?>
<article <?php if ($i == 5 || $i == 15 || $i == 25 || $i == 35 || $i == 45 || $i == 55) { ?>style="float:right;" <?php } ?>>
//do big item
</article>
} else {
<article>
//do small item
</article>
}
$i++;
}
Im also trying to work out how I can wrap every row of 5 items in another div to help with sizing? I was thinking another block of if ($i % 5 == 0) { may help with this but Im conscious of loading times and best practice too.
As always any help greatly appreciated!
Just introduce a second mod operator. eg.
if ($i % 5 == 0){
if ( ($i-5) % 10 == 0 ){
// so this will work for values of %i = 5, 15, 25, 35 ...
// big article
}else{
// small article
}
$i++ ; // as previous comment #javad pointed out. You are not incrementing i
for ($x = 0; $x <= 3; $x++) {
for ($y = 0; $y <= 4; $y++) {
if ($y == 0) {
mysql_query("insert into tb_weight_rate_management
(nation,zone_id,rate) values ('Domestic',1,'$del_100')");
}
}
}
hello i am little troubled using for loop....in the code above i have two loops ...1st loop will work 3 times and the the inside loop will work 4 times....
now when i click on submit button then it checks loop one and then enter second loop and inserts data 4 times in the database....which is wrong...i want if $y=0;
then it should insert data only once but it is inserting data 4 times can anyone please correct the above condition
You should use == instead of =. Like this:
if ($y == 0) {
the first loop runs 4 times, hence 4 inserts
In the inner loop you declare $y = 0 on every iteration of the outer loop
for each x iteration, there are one time y=0. and since x executes 4 times (0, 1, 2 and 3) y gets 0 value 4 times as well. if you want to do the insertion only once then you must add x value as well like if($y == 0 and x == 0).