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.