boo_lolly Posted January 4, 2007 Share Posted January 4, 2007 I have a while loop that outputs an HTML table. i need iit to alternate row colors. i'm sure there's many ways to skin this cat, but how would i incorporate it within the provided code below?[code]<?php while($s = mysql_fetch_array($sQuery)){ echo "\t<tr>\n". "\t\t<td>". $s[id] ."</td>\n". "\t\t<td>". $s[name] ."</td>\n". "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n". "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n". "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n". "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n". "\t\t</td>\n". "\t</tr>\n"; }?>[/code]many thanks. Link to comment https://forums.phpfreaks.com/topic/32888-alternating-row-colors/ Share on other sites More sharing options...
dbo Posted January 4, 2007 Share Posted January 4, 2007 [code]<?php $i = 0; while($s = mysql_fetch_array($sQuery)){ ++$i; $bgcolor = ""; if( $i % 2 == 0 ) { $bgcolor = "red"; } else { $bgcolor = "yellow"; } echo "\t<tr bgcolor=\"$bgcolor\">\n". "\t\t<td>". $s[id] ."</td>\n". "\t\t<td>". $s[name] ."</td>\n". "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n". "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n". "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n". "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n". "\t\t</td>\n". "\t</tr>\n"; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/32888-alternating-row-colors/#findComment-153092 Share on other sites More sharing options...
boo_lolly Posted January 4, 2007 Author Share Posted January 4, 2007 thanks dbo! i got this on my own... VERY similar.[code]<?php $color = 0; while($s = mysql_fetch_array($sQuery)){ if($color % 2 == 0){ $alt_color = "FFFFFF"; }else{ $alt_color = "DDDDDD"; } echo "\t<tr bgcolor=\"". $alt_color ."\">\n". "\t\t<td>". $s[id] ."</td>\n". "\t\t<td>". $s[name] ."</td>\n". "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n". "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n". "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n". "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n". "\t\t</td>\n". "\t</tr>\n"; $color++; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/32888-alternating-row-colors/#findComment-153161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.