Alternating rows, a.k.a. zebra striping is kind on the eyes. It allows the eye to easily follow rows in a table, or differentiate list items from each other. I see people asking how to do this all the time. And I’ve seen a lot of needlessly complex answers. This can be done in one line.
First, you have to decide what two CSS classes you are going to use for differentiation. You can go with two different classes, like
odd. Or, you can add something to a class, like
yourclass-odd, where one part stays the same. Finally, you can add something to the name of one class, like
yourclass-odd, do the following:
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '-even';
yourclass-odd do the following:
$even_odd = ( '-odd' != $even_odd ) ? '-odd' : '';
These should go at the top of your
while() loop, but inside the loop. Then, when you need to print the class, print this:
class="yourclass<php echo $even_odd; ?>"
What’s your favorite way of doing this?