0

I have used this code to toggle between classes for odd/even elements:

<li class="<?php echo ($key%2) ? "odd" : "even";>

How can I get every 3rd item, to add ex. classes 'one', 'two, 'three'?

2
  • stackoverflow.com/questions/1806582/… Look here,i think this might help you Commented Jun 26, 2015 at 13:39
  • I recommend you split the code from the presentation: e.g. set $class and then later do <li class="$class"> Commented Jun 26, 2015 at 13:45

6 Answers 6

2

There are many ways. Here is a "oneliner":

<li class="<?php $t=$key%3; echo ($t==0?"one":$t==1?"two":"three");>

If you get more than 3 items you might get more manageable code with a switch statement:

<?php

switch($key%4){
case(0): echo "one"; break;
case(1): echo "two"; break;
case(2): echo "three"; break;
case(3): echo "four"; break;
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Elegant, just what I was looking for, but was going in a different direction.
1

It's tough in a one-liner, but you could use a switch: <?php switch($key%3): case 0: echo "one"; break; case 1: echo "two"; break; case 2: echo "three"; break; ?>

To clean up your HTML files, you could put this switch statement into a function.

Comments

0

If you got your counter in your loop simple use modulo and check if it's 0. Then you can add the specific class.

if ($i % 3 == 0) { }

Comments

0
$classes = ['one', 'two', 'three'];

for (..) {
    printf('<li class="%s">', $classes[$key % count($classes)]);
}

Comments

0
<?php
$classes = array('first','second','third');
for ($i = 1; $i < 100; $i++) {
    echo $i.' class: ' .current($classes);    
    if(!next($classes))
    {
        // reset if there is end of array
        reset($classes);
    }    
    echo '<br/>';
}

elastic solution, You can have 3,4,5... classes by modifing $classes array only. Also is fast, don`t need to divide large numbers, just iterate small array with class listed in correct odrder

Comments

0

If you want clean HTML, define a function like below,

It will apply default class to every element, even class to every second element and third class to every third element, you can extend it as you like,

function apply_class($i)
{
    $every = 'default';
    $every_second = 'even';
    $every_third = 'third';

    $return [] = 'default';
    if ( $i%2 == 0 ) { $return [] = $every_second; }
    if ( $i%3 == 0 ) { $return [] = $every_third; }

    return implode(' ', $return);
}

Use it like,

<li class="<?php echo apply_class($i)?>" > </li>

Will render HTML like,

$i

1   <li class="default"> </li>
2   <li class="default even"> </li>
3   <li class="default third"> </li>
4   <li class="default even"> </li>
5   <li class="default"> </li>
6   <li class="default even third"> </li>
7   <li class="default"> </li>
8   <li class="default even"> </li>
9   <li class="default third"> </li>
10  <li class="default even"> </li>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.