Let's Do It
First off, let's get some data from our database. Let's assume we have a table called mytable with 3 fields: name, phonenumber, and age.
<?php
$db = mysql_connect("host","username","password") or die("Problem connecting");
mysql_select_db("dbname") or die("Problem selecting database");
$query = "SELECT * FROM mytable ORDER BY name";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
?>
Ok, we have our data from our database. Now we just need to open up our table and run a for loop for as many rows as we have and display the data. Then, we close the table.
<?php
echo "<TABLE BORDER=\"1\">\n";
echo "<TR bgcolor=\"lightblue\"><TD>Name</TD><TD>Phone</TD><TD>Age</TD></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"yellow\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD>".$row['name']."</TD><TD>".$row['phonenumber']."</TD><TD>".$row['age']."</TD>\n";
echo "</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>
That's it. Really. That's all there is to it. It's so simple that it is amazing. Now, get out there and fancy your tables up!
Source:
http://codewalkers.com/tutorials/6/2.html