Php Programming Code Examples
Php > Look and Feel Code Examples
Alternating Row Colors
Alternating Row Colors
<html>
<head>
<title>Alternating Row Colors</title>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<table width="300" height="400">
<?
// Connect to MySQL
mysql_connect ('localhost', 'username', 'password')
or die ("Can't connet to MySQL server.");
mysql_select_db ('dbname');
$result = mysql_query (SELECT * FROM table);
if ($row = mysql_fetch_array($result)) {
for ($i = 0; i < count($row); $i++) {
// Set Table Row Color
if ($i % 2) {
echo "<tr bgcolor=B0C4DE>";
} else {
echo "<tr bgcolor=FOF8FF>";
}
// Now display our table cell info
?>
<td><? echo $row["whatever"]; ?></td>
</tr>
<?
} else {
echo "<tr bgcolor=B0C4DE><td>Sorry, no records were found!</td></tr>";
}
?>
</table>
</body>
</html>