Alternating Table Row Colors
PHP Tutorial, Tutorials May 22nd, 2008This tutorial will provide you with a basic concept for creating alternating row colors. Using while loops and generating a list from the mysql table.
Firstly, Save the sql structure below as data.sql and upload the sources to your mysql database
# # Table structure for table data_list # CREATE TABLE `data_list` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM; # # Dumping data for table data_list # INSERT INTO `data_list` VALUES (1,'Ford'); INSERT INTO `data_list` VALUES (2,'Toyota'); INSERT INTO `data_list` VALUES (3,'Mitsubishi'); INSERT INTO `data_list` VALUES (4,'Honda'); INSERT INTO `data_list` VALUES (5,'Chevrolet'); INSERT INTO `data_list` VALUES (6,'Mercidez Benz'); INSERT INTO `data_list` VALUES (7,'Isuzu'); INSERT INTO `data_list` VALUES (8,'Jeep'); INSERT INTO `data_list` VALUES (9,'Suzuki'); |
Secondly, Once you have done creating and uploading your database structure. You can start working on table.php.
1 2 3 4 5 6 7 8 9 | <?php $dbhost = 'localhost'; // Change to your host. $dbuser = 'USERNAME'; // Change to your user. $dbpass = 'PASSWORD'; // Change to your password. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); // $dbname relates to the database name you gave when setting up the mysql settings. $dbname = 'DATABASENAME'; mysql_select_db($dbname); ?> |
10 11 12 13 | <table border="1"><tr> <td bgcolor="#FF9900"><strong>No.</strong></td> <td bgcolor="#FF9900"><strong>Company</strong></td> </tr> |
This section above is the start of the table with its headers
The last section above is self explanatory.
To view a live Demo : Click Here!
To Download the script above : Click Here!
May 28th, 2008 at 9:49 pm
Wouldn’t it be easier to do just this?
<?php
// Get data from mysql table.
$sql = “SELECT * FROM data_list”;
$result = mysql_query($sql) or die(”Error connecting to mysql”);
// Make a variable “$num” with a value “1″.
$num=1;
// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){
// Plus 1 at $num.
$num++;
// Use modulus by 2 in $num value and set the value of “$bg” if result equal 0 or none.
if(($num%2)!=0){
// Prints the First part of the row.
$bg=”#FFCC00″;
}else{
// Prints the SECOND part of the row.
$bg=”#FFFF00″;
} // Close (if).
echo “$row[id]$row[name]“;
} // End while loop.
mysql_close(); // Close database connection.
?>
May 28th, 2008 at 9:51 pm
Nevermind, your textbox just stripped my HTML tags off!
June 10th, 2008 at 1:29 pm
I think that, that is the easiest way
:
…
$colors = array(”#DDDDDD”, “#CCCCCC”);
while($row=mysql_fetch_assoc($result)){
…
$n++;
$bg = $colors[$n%2];
…
}
…
June 10th, 2008 at 3:34 pm
“if ($bg==’#FFCC00′){$bg=’#FFFF00′;}”
“else ($bg=’#FFCC00′;}”
June 14th, 2008 at 3:19 pm
I think there are a number of inefficiencies here. Anyways, I think it has been established that the fastest way to do row coloring would be:
$n = 0;
loop
{
//oscillatory value
$n = 1 - $n;
if($n == 1)echo $your_html_code1
else echo $your_html_code2
}
This is much faster than using the modulus function.
du
June 24th, 2008 at 3:39 pm
If we want to use CSS for the row colors and have valid output can we still use this method?