隔行换色的表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?
$mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "yourpassword";
$mysql_database = "yourdatabase";
$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
?>
<style. type="text/css">
tr.row1{background-color:gray;}
tr.row2{background-color:white;}
</style>
<?
function table_row_format(&$row_counter){
//return row class for a row
if($row_counter & 1){
$row_color="row2";
} else {
$row_color="row1";
}
$row_counter++;
return $row_color;
}
//这里记数在函数进行,和表格长度就无关了
//$sql = "SELECT product_name FROM `product_info`";
$sql = "SELECT product_name FROM `product_info`";
$result=mysql_db_query( $mysql_database, $sql,$conn);
echo "<table>";
$i=0;
while($row=mysql_fetch_array($result)){
//print resuilt
$row_class = table_row_format($i);
echo "<tr class= \"$row_class\"><td>$row[product_name]</td></tr>";
}
//关键是根据$row_class变量选择不同的css class
echo "<table>";
?>
<meta. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>un</title>
</head>
<body>
</body>
</html>