Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Alternating Row Color

Alternating Row Color

Learn how to make an "alternating row color" script.

<?php
/* CONNECTION VARIABLES */
$db_hostname = "yourhostname"; // Usually "localhost"
$db_username = "yourusername";
$db_password = "yourpassword";
$db_name = "yourdatabasename";
$table = "yourtable";

$conn = mysql_connect($db_hostname, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name, $conn) or die(mysql_error());
$query = "SELECT * FROM $table";
$result = @mysql_query($query) or die("Could not access database.");
$numofrows = @mysql_num_rows($result);

print '<table width="650" border="0" cellpadding="2" cellspacing="1">\n';
print '<TR><td width="50%">Name</td><td width="50%">Email</td></TR>\n';
for($i = 0; $i < $numofrows; $i++) 
	{
	$row = @mysql_fetch_array($result);
	if($i % 2) 
		{ 
		print '<TR bgcolor="#333333">\n';
		} 
	else 
		{ 
		print '<TR bgcolor="#444444">\n';
		}
	print "<td>" . $row['name']."</td><td>" . $row['email']."</td>\n";
	print "</TR>\n";
	}
print "</table>\n";
?>


$db_hostname = "yourhostname"; // Usually "localhost"

This variable is your site's hostname. Which is usaully "localhost".



$db_username = "yourusername";

The username your host assigned to you, or you picked if you set up your server.



$db_password = "yourpassword";

The password you were provided by your host or the one you picked.



$db_name = "yourdatabasename";

The name of your database.



$conn = mysql_connect($db_hostname, $db_username, $db_password) or die(mysql_error());

"$conn" is the variable that holds all the connection information. "mysql_connect" is a predefined function in PHP that allows you to connect to MySQL. the "or die" part is saying if the connection to SQL fails kill the script and print the error that was encountered. "mysql_error()" prints the specific error.



mysql_select_db($db_name, $conn) or die(mysql_error());

The "mysql_select_db" selects the database that is filled in the "$db_name" variable. The "$conn" variable holds all the connection information that was filled at the beginning of this script. Once again if this part of the script fails it will print why using "mysql_error()".



$numofrows = @mysql_num_rows($result);

This is reading the number of rows that exist in the database.



print '<table width="650" border="0" cellpadding="2" cellspacing="1">\n';

This is just the source code for the start of a table.



print '<TR><td width="50%">Name</td><td width="50%">Email</td></TR>\n';

This is the top row that displays the row names. No explanation needed. Right?



for($i = 0; $i < $numofrows; $i++) 
	{

Now back to the "real" code, this is the start of a for loop. This is saying keep running this script until the value of $i is equal to $numofrows variable. "$i = 0" is to automatically set the value of $i equal to 0. "$i++" is to increment "add to" the value of $i every time it passes by the start of the loop.



$row = @mysql_fetch_array($result);

this associates the variable $row to a piece of the array. Example: $row['name'] would read out name in the row "name".



if($i % 2) 
	{ 
	print '<TR bgcolor="#333333">\n';
	} 
}

This says if the value of $i divisible by 2 results in a remainder, print the "<tr>" with the specified background color.



else 
	{ 
	print '<TR bgcolor="#444444">\n';
	}

This is printed if the value of $i is divided by 2 with no remainder.



print "<td>" . $row['name']."</td><td>" . $row['email']."</tr>\n";

Now we get to the data that is displayed. In this part you will see the example I gave about the $row variable. This is outputting a "<td>" and displaying the data that was gathered with the "mysql_fetch_array".



print "</TR>\n";
}

Output a closing "</tr>" to close the table row.



print "</table>\n";

Output a closing "</table>" to close the table.



That is the end of the alternating row color tutorial. Now you should have a better understanding about these.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Alternating Row Color

 Gr3g0r  Sat Jul 8, 2006 3:15 am  
Well done. On the other hand, I found another way. It's not so complex as this, just simply stores your colors in table and then directly alternate them into your output html code:

$query_color = "SELECT * FROM colors ORDER BY RAND() LIMIT 1";
$result = mysql_query($query_color);
$r = mysql_fetch_object($result);
";
else echo "";
echo

}
 Craig Forant  Mon Mar 26, 2007 4:39 pm  
or...do this...I personally don't like using the for statement

$rowcount = 0;
while($row = mysql_fetch_array($query){
$rowcount++;
if ($rowcount % 2) echo "
Cell1Cell2Cell3
";
}
else {
print "";
}

 Hedley Phillips  Tue Jan 29, 2008 9:37 am  
We use the same as Craig:

my $row_count = 0;
while($query_handle->fetch()) {
if ($rowcount % 2) {
print "
 Hedley Phillips  Tue Jan 29, 2008 9:39 am  
Damn, the form stripped everything off after the print statement, so here it is again...
my $row_count = 0;
while($query_handle->fetch()) {
if ($rowcount % 2) {
print one colour
}
else {
print the other colour
}
 Busby SEO Challenge  Sun Aug 3, 2008 12:43 am  
Your site is great! Thanks for sharing it with us..
 angel  Thu Jun 9, 2011 10:31 pm  
such a good script i have looking for something very simple.

thank so much guy u r amazing

one of you would post some examples for pages a query???
 ziad hashim  Sat Jun 18, 2011 2:34 am  
Thank you, Its people like you that makes learning Php both simple and fun, my layout just got nicer, thanks to you. Do give us more
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles