verN Posted April 13, 2007 Share Posted April 13, 2007 I have a table and would like the row to change colours. The table uses two colours white and green. The frist row in the table should be green then white then green then white and so on. How would i go about doing this thanks. I am using while() tio get the rows and storing it in a variable thanks in advance Link to comment https://forums.phpfreaks.com/topic/46844-solved-table-row-colour/ Share on other sites More sharing options...
Guest prozente Posted April 13, 2007 Share Posted April 13, 2007 something like this should do the trick $alt = 1; //while(etc etc){ echo '<tr class="row'.$alt.'"><td>etc etc</td></tr>'; $alt == 2 ? $alt = 1 : $alt = 2; } Link to comment https://forums.phpfreaks.com/topic/46844-solved-table-row-colour/#findComment-228319 Share on other sites More sharing options...
tauchai83 Posted April 13, 2007 Share Posted April 13, 2007 <?php $row_counter=0; while($row=mysql_fetch_assoc($result)) { blah blah blah; } $row_counter++; if($row_counter%2==0) { echo "\n<tr bgcolor=green>\n"; } else { echo "\n<tr bgcolor=white>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/46844-solved-table-row-colour/#findComment-228321 Share on other sites More sharing options...
Guest prozente Posted April 13, 2007 Share Posted April 13, 2007 tauchai83, one thing about your approach is the more rows you go through the more memory would be taken by PHP as the variable got larger. Aside from this you may also try shortening your code by using echo "\n<tr bgcolor='".(($row_counter%2==0) ? 'green' : 'white' )."'>\n"; instead of the if statement Link to comment https://forums.phpfreaks.com/topic/46844-solved-table-row-colour/#findComment-228326 Share on other sites More sharing options...
tauchai83 Posted April 13, 2007 Share Posted April 13, 2007 thanks for the reminder. Many ways to do that. Thanks anyway. Programming is all about creativity and skill. Maybe at this stage, your skill is better. Hehe. cheerss.. Link to comment https://forums.phpfreaks.com/topic/46844-solved-table-row-colour/#findComment-228329 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.