PDA

View Full Version : Alternative Row Color


eyecandy
06-16-2003, 07:08 AM
hey, im pretty new at PHP and mysql coding and i was just wondering if anyone knew how to alternate the color of a table row when it's being looped/repeated when getting data from a database.

:(

duniyadnd
06-16-2003, 07:20 AM
Use modulus (%)

<table>

<?
$i = 1;
$max = 50;

while ($i < $max)
{
if ($i%2 == 0)
{
?>
<tr bgcolor="#aaaaaa"><td>
</td></tr>
<?
}//end if
else
{
?>
<tr bgcolor="#ffffff"><td>
</td></tr>
<?
}//end else
$i++;
}//end while
</table>


What modulos does is divides the number you have by the modulos: example $i = 4 (making it 4/2) and takes the remainder (ie. 0). If the modulos (in this case 0) is equal to what we are looking for,background color #aaaaaa, else #ffffff

Hope that made sense

Ökii
06-16-2003, 11:15 AM
or you can use a ternery construct to determine which string to echo

$r_count = 0;
while(..........)
{
echo '<tr style="background-color:#' .((++$r_count %2 == 0) ? 'FFFFFF' : 'AAAAAA'). '"><td>.......</td></tr>';
}

which increments the $r_count at each iteration and echoes FFFFFF if the modulus remainder is zero and AAAAAA if non zero.

eyecandy
06-16-2003, 03:25 PM
:)

sweet thanks alot guys il give it a go...