<div class="example-class<?php echo ($xyz++%2); ?>">
Used inside a loop, class names would be named .example-class0 and .example-class1 alternating. Increasing the “2” number allows you to increment in thirds or fourths or whatever you need:
class="class<?php echo ($xyz++%3); ?>"
Classes: class0 class1 class2 class0 class1 class2
Change number to 4, this is the result: class0 class1 class2 class3 class0 class1 class2 class3
FYI, the 2nd example should read:
class="class"
doh:
class="class\"
ok I give up. Just add 3 instead of 2 :P
Easiest method I have ever seen, thank you!
That was easy!
Thanks~!
Wow. That rocks! Thank you so much!
thanku :) heaps :)
It´s very Good
Awesome,
The easiest one so far. So we do not have to use the old
one…. Saves time :)
what about this technique, i think its much faster then taking decision on if statement.
$class = ($class==’class1′) ? “class2” : “class1” ;
That’s still a conditional statement
for($i=1; $i<=10; $i++){
if($i%2){ echo "class"; } else { echo "class"}
}
Simple example can be use in any loop even in wordpress.
Thanks a lot Jessica this is exactly what i was looking for.
And… if you php.ini has enabled the directive “short open tag” then the snippet becomes shortest ever!! hehe
I would say that the easiest way to use two different classes (or more) without using the if-statement is:
<?
$xyz=0;
$class=array('odd','even'); ?>
<div class="<?=$class[$xyz++%2] ?>
$loopNumber= 4;
$i =1;
$x = array(‘Sed’, ‘ut’, ‘perspiciatis’, ‘unde omnis’, ‘iste’, ‘natus’, ‘error’, ‘sit’, ‘voluptatem’, ‘accusantium’, ‘doloremque’, ‘laudantium’);
foreach($x AS $k => $v)
{
echo ”
“.$v.”
“;
$i = ($i < $loopNumber) ? ++$i : 1;
}
result:
That’s what the top part should have read….
Thanks. This example it´s very good for designs with css and wordpress
All of mine say ‘0’…. ?