Chris Sedlmayr Real World Development

16Mar/101

Zebra Striping with PHP The Easy Way

Using one of the built in Math functions of PHP we can easily create zebra striped lists.
Let's say we have an array of entries stored within the $comments variable.
All we have to do with this is to use the fmod function and assign a css class that alternates per entry.

<ul>
  <?php foreach ($comments as $int => $comment): ?>
    <li class="<?php echo fmod($int, 2) ? 'even' : 'odd' ?>">
  <?php endforeach; ?>
</ul>

Then just give the even and odd classes different colours to suit your layout.

li.even {
  background-color:#FFFFFF;
  color:#000000;
}
 
li.odd {
  background-color:#000000;
  color:#FFFFFF;
}

And you're done!