Add php code each 12 items starting from 2nd item - php

I have some loop. How can i optimize this code that it would be executed each 12 items starting from 2nd item. I'm using hardcoded code for each of +12 item, but of course this is not a good solution :) I know it's easy to do and i was trying to do it with modulus operator, however it worked somehow incorrectly.
<?php if( ($counter == 2) || ($counter == 14) || ($counter == 26) || ($counter == 38) || ($counter == 50) || ($counter == 62) || ($counter == 74) || ($counter == 86) || ($counter == 98 .... ?>
Thanks for help!

Answer
Take a look at the Modulus operator (%):
<?php
if ( ( $counter - 2 ) % 12 == 0 ) {
//....
}
?>
Explanation
The Modulus operator ($a % $b) is the remainder of $a divided by $b.
$counter - 2 - as you're starting with an offset of 2, remove that from $counter
% 12 - will return the remainder of $counter - 2 divided by 12
== 0 - if the above returns 0, you know that it is exactly divisible

Related

How to target block 1, 3, 5 etc. in a loop with PHP counter

I'm trying to add a div class "row" to my 1st, 3rd, 5th block etc.
I have the basics down, but it keeps targeting blocks 2,4,6 etc. I've been bashing my head for an hour because I assume it's really simple, but knowing little to none PHP i'm just not getting it right.
This is what I have:
<?php $counter = 1; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($counter % 2 == 0) {
echo "<div class=\"row\">ROW";
}
?>
<p>Title #<?php print "$counter"; ?></p>
<?php
if ($counter % 2 == 0) {
echo "</div>";
}
?>
<?php $counter ++; ?>
Anyone who can point me in the right direction? Thanks in advance!
The reason why it's targeting rows even numbers it's because you're doing
$counter % 2 == 0
You need to understand what the % does. The % is a Modulus Operator, which will get the remainder of $x divided by $y as stated in the link.
So if you want to get the odd numbers, you'll have to do
$counter % 2 != 0
Or
$counter % 2 == 1
Use $counter % 2 == 1 to target odd numbers

calculate specific values of counter inside foreach loop

I am trying to calculate specific values of a counter in my foreach loop.
I have this if statement in my code
if ( $i == 21 || $i == 41 || $i == 61 || $i == 81 || $i == 101 )
which are equal to
($i * 20) + 1
Instead of writing all these values (21,41,61,81...) I want to create a formula for my code but I couldn't figure out what the result should be equal to inside my if statement
Use modulus:
if ($i % 20 == 1) { ...
http://php.net/manual/en/language.operators.arithmetic.php
Look for the remainder after dividing by 20 using the % operator (modulus).
if ($i%20 == 1)
{
// do stuff
}

if $count is a multiple of 3 + 1 in php

In php i want to calculate if my count in a foreach loop is a multiple of 3 + 1.
For example if it is 4,7,10,13,16 you get the point.
if( $count % 3 != 0)
This is the closest ive come to finding an answer
What about this?
if ($count % 3 == 1)
This should work:
if ( ($count - 1) % 3 == 0)
Try maybe this:
if ( $count % 3 == 1)

Simple "== x OR y" in PHP

I have a loop in which I check for numbers. Something like this, more or less:
<?php $counter = 0; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $counter++; ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php if ($counter == (2 || 4)) : ?>
// DO SOMETHING
<?php endif; // counter ?>
...
But $counter ==(2||4) returns true for every number (1, 2, 3, 4 etc.). I also tried:
$counter == 2 || 4
$counter === 2 || 4
$counter == (2 or 4)
But to no avail. The first and last one also return true for every natural number and the second one never returns true.
An alternate solution would be to do the following:
$allowed = array(2, 4);
if (in_array($counter, $allowed)) {
// good value
}
The advantage of this is that you can extend the list of allowed values very easily, maybe even placing them in an external config.
simply replace
if ($counter == (2 || 4)) :
with
if ($counter == 2 || $counter == 4) :
see also the manual: http://www.php.net/manual/en/control-structures.if.php
If you want to do something when the counter is even, you can use the following:
<?php if ($counter % 2 === 0) : ?>
// do something
// will match 0, 2, 4, 6, ...
<?php endif; ?>
Its not possible to check for multiple values inside a condition like that.
This is one reason why php sucks... you're are forced to use two conditions, like
if($counter == 2 || $counter == 4)
or if you want to trigger your conditions on even numbers
if($counter % 2 == 0)
someone mentioned in_array() but you should know that arrays are very expensive in php in terms of resources.
The expression (2 || 4) will always return true. This is because both operands to the || operator are non zero valued constants. It is essentially equivalent to (true || true) which is always true as is (false || true).
the expression
$counter == (2 || 4)
will yield true except when $counter is 0
You want to write
($counter === 2 || $counter === 4)
note that you should always use === instead of == because the latter performs type coercion on its operands.

Why is my conditional echo-ing the wrong thing

I have something like
just a snipplet
$i = 1; while (...) {
echo ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ? 'class="last-col"' : '';
$i++;
}
but even when $i % 5 == 1, I will get class="last-col" echo-ed is my logic right?
This is actually a CSS fix for IE so that I wont need to use nth-child. I am trying to target the 1st and last columns of my grid which contains 5 col/row
The ?: operator is left-associative, i.e. you have
echo ( ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ) ? 'class="last-col"' : '';
See http://php.net/manual/en/language.operators.comparison.php
It is best not to nest ternary operators.
Better use if / elseif / else constructs, they are more legible.
adding parenthesis helps:
echo (($i % 5 == 1) ? 'class="first-col"' : (($i % 5 == 0) ? 'class="last-col"' : ''));
For a start you're using nested ternary operations. I'd at least use brackets around the individual conditions to make it obvious what should be carried out first.

Categories