How to alternate row colors in a table with PHP?
| Important: The following is a text only archive! For full features; Go to How to alternate row colors in a table with PHP? |
posted by Lensy How can I show rows from a database in a table with alternating colors? Thanks in advance...
posted by 2Guns
To alternate between 2 colors you can use the below code within a loop
$bgcolor = (++$i & 1) ? '#ffffff' : '#000000';
Now let's say you wanted to alternate between four colors, you could do
something like (within a loop again);
$colors = array('black','green','blue','yellow');
$bgcolor = $colors[$i++ % 4];
posted by 2Guns
Here's simple example using the code above;
while( $row = mysql_fetch_array($result) )
{
$bgcolor = (++$i & 1) ? '#ffffff' : '#000000';
echo "
}