I have a PHP loop that displays data for a particular posting on my website. What I want to do is dynamically assign either and even/odd CSS class for the number of row that is returned. For example,
first row returned - odd class
second row returned - even class
third row - odd class
This will allow me to assign different background colors to keep the data visually separate.
I should clarify – this data isn’t being returned in a table.
while ($row = mysql_fetch_assoc($result)) {
echo "
<div class=\"songContainer\" id=\"" . $row['trackid'] . "\">
Depending on which browsers you have to target, you could just do that with CSS:
You can even do more complex things with this pseudo-class: http://www.quirksmode.org/css/nthchild.html
If you really need PHP, use a modulo: