Page 1 of 1

help me implementing a third row

Posted: Thu Feb 05, 2009 4:03 pm
by PHPeanuts
Hello guys. I'm just trying to alternate my background in the comments of my site - three colours which will repeat -, and I'm a kind of newbie in PHP even in the basics so I need your help.

I just get it working with two. Here it is what I have by now :

Code: Select all

 
    <ol>
<?php $i = 0; ?>
    <?php foreach ($comments as $comment) : ?>
        <?php $i++; ?>
            <li <?php if($i&1) { echo 'class="odd"';} else {echo 'class="even"';} ?>> 
                <?php comment_text() ?>
            </li>
          <?php 
        endforeach;
        ?>
    </ol>
Then in my CSS I set up an .odd background and a .even background.

How could I add a third row here ?

Thanks

Re: help me implementing a third row

Posted: Thu Feb 05, 2009 4:16 pm
by mickeyunderscore
You could use the modulus operator instead:

Code: Select all

if($i % 3 == 0){
    echo '1';
}elseif($i % 3 == 1){
    echo '2';
}else{
    echo '3';
}

Re: help me implementing a third row

Posted: Thu Feb 05, 2009 4:38 pm
by PHPeanuts
Thanks a lot Mick, that worked right away. :D

My final code :

Code: Select all

 
<ol>
<?php $i = 0; ?>
<?php foreach ($comments as $comment) : ?>
<?php $i++; ?>
    <li <?php if($i % 3 == 0){echo 'class="grey"';}elseif($i % 3 == 1){echo 'class="purple"';}else{echo 'class="yellow"';} ?>> 
        <?php comment_text() ?>
    </li>
<?php endforeach; ?>
</ol>