hello pros,
I want to alternate between 2 background colors in my HTML table rows. I've seen some scripts online, but I cannot get them to behave correctly!
Can someone please give me a simple coding example on how to acheive this effect?
Thanks!
Hi,
try following:
$result = MYSQL_QUERY("SELECT * FROM table"); echo "<center>\n<table>\n<tr>\n"; while ($field = mysql_fetch_field($result)) { echo "<th>$field->name</th>\n"; } echo "</tr>"; $j = 1; while($row = mysql_fetch_row($result)) { echo "<tr>"; for($i=0; $i<mysql_num_fields($result); $i++) { // alternate the colors if($j%2 == 0) { $color = "#EEEEEE"; } else { $color = "#FFFFFF"; } echo "<td bgcolor=\"".$color."\">".nl2br(htmlentities($row[$i]))."</td>"; } $j++; echo "</tr>\n"; } echo "</table>\n</center>\n<BR>\n<BR>";
wizkid
http://www.spoono.com/tutorials/php/alternating_row_colors/