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! Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/ 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. Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/#findComment-1200500 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? Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/#findComment-1200513 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 Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/#findComment-1200526 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. Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/#findComment-1200527 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.. Link to comment https://forums.phpfreaks.com/topic/233472-php-mysql-how-to-alternate-table-row-colors/#findComment-1200533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.