Alternating Rows of mysql data in different colors

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Groundlevel
Forum Newbie
Posts: 1
Joined: Sat Aug 16, 2003 8:31 pm

Alternating Rows of mysql data in different colors

Post by Groundlevel »

Hi--

I am trying to write an if statement which will check if a row returned from a mysql query is odd or even, and then set the bg color of the <tr> Table Row of the html table to be built from the rows, to be set to an alternating color.

I have several different columns to be displayed, and I have it set as this now:

Code: Select all

<?php

while($row = mysql_fetch_array($query)) {

{

?> 

<tr bgcolor="#eff3fe"> 

<td width="61" height="21"> 

<p style="margin-left: 10; margin-right: 10"> 

<font face="Verdana" size="1"> 

<?php echo $row["til_id"]; ?> 

</font> 

</td> 



<td width="86" height="21"> 

<p style="margin-left: 10"> 

<font face="Verdana" size="1"> 

<?php echo $row["category"]; ?> 

</font> 

</td> 

<td width="33%" height="21"> 

<p style="margin-left: 10"> 

<font face="Verdana" size="1"> 

<?php echo $row["question"]; ?> 

</font> 

</td> 

<td align="left" width="78" height="21"> 

<p style="margin-left: 10"> 

<font face="Verdana" size="1"> 

<?php echo $row["created"]; ?> 

</font> 

</td> 
?>
...and a few more. So I guess just a line at the top changing the <tr bgcolor="#eff3fe"> would be best... :)

Thank You!

All My Best,
Jeffrey
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try putting this just before your while loop:

Code: Select all

$i = 0;
Then just after the while loop put:

Code: Select all

$bgcolor = (++$i % 2 == 1) ? '#eff3fe' : '#ffffcc';
Then you can replace:

Code: Select all

<tr bgcolor="#eff3fe">
with

Code: Select all

<tr bgcolor="<?php echo $bgcolor; ? >">
(note that there shouldn't be a space between the ? and the > at the end)

Mac
Post Reply