xtrad Posted August 3, 2010 Share Posted August 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209703-colouring-alternate-table-rows/ Share on other sites More sharing options...
systemick Posted August 3, 2010 Share Posted August 3, 2010 Before you output the html for the table simply insert $counter = 0; Quote Link to comment https://forums.phpfreaks.com/topic/209703-colouring-alternate-table-rows/#findComment-1094738 Share on other sites More sharing options...
xtrad Posted August 4, 2010 Author Share Posted August 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209703-colouring-alternate-table-rows/#findComment-1094963 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2010 Share Posted August 4, 2010 You are resetting the counter inside of your loop, so of course all the colors will be the same. systemick told you to reset the counter before you output the html for the table. Quote Link to comment https://forums.phpfreaks.com/topic/209703-colouring-alternate-table-rows/#findComment-1095046 Share on other sites More sharing options...
xtrad Posted August 4, 2010 Author Share Posted August 4, 2010 Thanks for the reply. I've got it working perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/209703-colouring-alternate-table-rows/#findComment-1095053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.