jalmz Posted April 12, 2011 Share Posted April 12, 2011 Hi guys, I would like to know on How to alternate table row colors in Php Mysql result? CSS or Html? Thanks <? $result = mysql_query("SELECT * FROM tbl_sta WHERE fiesta_date > '$now' ORDER BY sta_date LIMIT 5"); echo "<table id='table1' cellspacing='1' cellpadding='3'> <tr> <th>Upcoming Fiesta</th> </tr>"; while($row = mysql_fetch_array($result)){ $date = date("l, F j ",strtotime($row["sta_date"])); echo "<tr>"; echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br /> $date </td>"; echo "</tr>"; } echo "</table>"; ?> Thank you.. more power phpfreaks! Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 $i = 1; $color1 = "#ff0000"; $color2 = "#00ff00"; while($row = mysql_fetch_array($result)){ $date = date("l, F j ",strtotime($row["sta_date"])); if($i%2 == 1) $color = $color1; else $color = $color2; echo "<tr style='background-color: $color'>"; echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br /> $date </td>"; echo "</tr>"; $i++; } Very flashy colors, but you get the idea. Quote Link to comment Share on other sites More sharing options...
jalmz Posted April 12, 2011 Author Share Posted April 12, 2011 Thank you ! it works like a charm! How can we give points to the contributors of this website? Quote Link to comment Share on other sites More sharing options...
jalmz Posted April 12, 2011 Author Share Posted April 12, 2011 what about using CSS class? alt1 alt2 Quote Link to comment Share on other sites More sharing options...
dcro2 Posted April 12, 2011 Share Posted April 12, 2011 Same concept $i = 1; while($row = mysql_fetch_array($result)){ $date = date("l, F j ",strtotime($row["sta_date"])); if($i%2 == 1) $trclass = "alt1"; else $trclass = "alt2"; echo "<tr class='$trclass'>"; echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br /> $date </td>"; echo "</tr>"; $i++; } There's no points system on this forum by the way. Quote Link to comment Share on other sites More sharing options...
jalmz Posted April 12, 2011 Author Share Posted April 12, 2011 ok.. The power of modulo.. Thank you.. Quote Link to comment 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.