Tables are really usefull for displaying data from a database in an arranged manner. It will be always pretty nice if we put some methods to distinguish each rows from other, so that every row will get equal importance. This simple php code will help you to do the trick.Happy coding.
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
<title>Alternative table row colours</title>
</head><body>
<?
// Connect database.
mysql_connect(
"localhost"
,
"username"
,
"password"
);
mysql_select_db(
"database"
);
// Get data records from table.
$result
=mysql_query(
"select * from table name"
);
?>
<table border=
"1"
>
<tr>
<td bgcolor=
"#FFCCCC"
><strong>ID.</strong></td>
<td bgcolor=
"#FFCCCC"
<strong>Table</strong></td>
</tr>
<?
// Make a variable "$count" with a value "1".
$count
=1;
// Do while loop for out put records.
while
(
$row
=mysql_fetch_assoc(
$result
)){
// Plus 1 at $count.
$count
++;
// Use modulus by 2 in $count value and set the value of "$background" if result equal 0 or not.
if
((
$count
%2)!=0){
$background
=
"#D3E5FF"
;
}
else
{
$background
=
"#CFCFFF"
;
}
?>
<tr bgcolor=
"<? echo $background; ?>"
>
<td><?
echo
$count
; ?></td>
<td><?
echo
$row
[
'table_name'
]; ?></td>
</tr>
<?
// End while loop.
}
// Close database connection.
mysql_close();
?>
</table>
</body>
</html>
No comments:
Post a Comment