PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

ALTERNATING ROW COLORS WITH PHP AND MYSQL - METHOD 1

This tutorial shows you how to display your mysql results in a table that has alternating row colors. This is a nice, snazzy visual effect to use on your site.

There are two methods to do this. This is the first method, the second one is slightly more complex and can be found here.

I prefer this method as it is simpler.

We first ensure we have our database connection script. I call mine open_db.php and it contains the username, password and database connection information. I then include the open_db.php at the top of this script.

<?

include("open_db.php");

We then select our data out of the database.

$query = "SELECT keywords FROM google_search";
$result = mysql_query($query) or die(mysql_error());

We declare a variable $RowCounter and intiate it to a value of 0.

$RowCounter=0;

We then echo the opening table structure as follows:

echo "<table cellpadding='0' cellspacing='0'>";

We now retrieve the results from our database in a while loop:

while($row = mysql_fetch_array($result))
{
$keywords=$row['keywords'];

We now say that when the $RowCounter is divided by 2 and there are remainders, then our row color is red, otherwise it is yellow. (Obviously, you can change the colors to your own.)

if($RowCounter % 2)
{
$RowColor="bgcolor='red'";
}
else
{
$RowColor="bgcolor='yellow'";
}

We now print our row assigning it the bgcolor from variable $RowColor and echo our data (which are keywords here).

echo "<tr ".$RowColor."><td>";
echo $keywords;
echo "</td></tr>";

We now increment $RowCounter variable by 1.

$RowCounter++;

We close the loop.

}

We close the table.

echo "</table>";

?>

Each time the while loop executes, $RowCounter increments in value by 1 and a test is performed at the top of the while loop to see if can be divisible by 2 with a remainder. Then the if statement allocates the color accordingly.

The full script is below:

<?

include("open_db.php");

$query = "SELECT keywords FROM google_search";
$result = mysql_query($query) or die(mysql_error());

$RowCounter=0;

echo "<table cellpadding='0' cellspacing='0'>";

while($row = mysql_fetch_array($result))
{
$keywords=$row['keywords'];

if($RowCounter % 2)
{
$RowColor="bgcolor='red'";
}
else
{
$RowColor="bgcolor='yellow'";
}

echo "<tr ".$RowColor."><td>";
echo $keywords;
echo "</td></tr>";

$RowCounter++;

}

echo "</table>";

?>

A Slightly More Complex Method

Home | Privacy Policy | Terms Of Use | Contact Us | Work At Home | Online Jobs | Smart Info | Online Jobs | Data Entry Jobs | Paid Surveys | Info | Info | Typing Jobs | Article Archive | Forum Posting Jobs