Jump to content

Colouring alternate table rows


xtrad

Recommended Posts

Hi,

 

I'm using a table to list results from a MySQL query.

 

I wanted to make the rows of the table alternate colours and found the following code on this website which does the job perfectly:

 

$counter++;
$bgcolor = ($counter % 2)?"#e8edff":"#ffffff";

echo "<tr class='border'>\n
	<td bgcolor='".$bgcolor."'>$n</td>\n
	<td bgcolor='".$bgcolor."'>$player</td>\n
	<td bgcolor='".$bgcolor."'>$score</td>\n
	</tr>\n";

 

On my webpage i have six tables listing results from six MySQL queries and i use the above code after each query to construct the table and alternately colour the rows blue then white.

 

My problem arises when there are an odd number of rows in the first table.  This results in the first row of the second table being coloured white, not blue.

 

I would like the first row of each table to be blue.

 

Is there a simple way to reset the counter for each table?  Any advice much appreciated.

 

Thanks.

Link to comment
Share on other sites

Thanks for the quick reply.

 

It seems like a simple solution but when i insert $counter = 0; before the html as below...

 

$counter++;
$bgcolor = ($counter % 2)?"#e8edff":"#ffffff";

$counter = 0;

echo "<tr class='border'>\n
	<td class='firstcolumn' bgcolor='".$bgcolor."'>$n</td>\n
	<td bgcolor='".$bgcolor."'>$player</td>\n
	<td class='scores' bgcolor='".$bgcolor."'>$score</td>\n
	</tr>\n";

 

... this results in all of the table's rows being blue.  I also tried inserting $counter = 0; before $counter++; but this also results in all table rows being blue.

 

So it seems that the counter is being reset for every row, but i only want it reset at the beginning of each table.

 

Again, any suggestions will be appreciated.

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.