Home

See the related posts

PHP Warning: Call-time pass-by-reference has been deprecated    Working with Database (MySQL)    Cookies in PHP    Session Management Functions in php    Join array elements with a string – implode   

Alternating Table Row Colors using php

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 :)

<html xmlns="http://www.w3.org/1999/xhtml">
<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();
<a href="http://samplephpcodes.com">Sample PHP Codes</a>
?>
</table>
</body>
</html>

Home

See the related posts

PHP Warning: Call-time pass-by-reference has been deprecated    Working with Database (MySQL)    Cookies in PHP    Session Management Functions in php    Join array elements with a string – implode   

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment