PHP Diary
| scriptschool.com | PHP Scripts | TD Scripts.comThe third step - sort and display the results
The last step is to sort and display the results which can be accomplished by the following SQL statement: SELECT:
SELECT * FROM avg_tally ORDER BY average DESC;
You've probably seen sites that alternate the colors inside a table with results. It is actually a lot easier than it looks, actually when pulling this information from a mySQL table. Let's convert this to our PHP script and then create an alternating colorized table with the results in it. Let's creat a table which looks like this:
Rank |
Rating |
Diary Date |
Description of Diary Page |
1 |
|||
2 |
Now admittedly my choice of colors may be way off base, lol, but I'm not a designer, I'm a programmer, so you can change the color scheme to whatever works for you. Here's the HTML code to make the table above:
<
table border="0" width="100%">Now let's create the PHP code to insert the sorted contents
from the mySQL database into this table and alternate the colors of the tables. Color 1 is
#D8DBFE and color 2 is #A6ACFD. Here is the code to create this:
<table border="0" width="100%">
<tr>
<td width="6%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Rank</font></small></td>
<td width="7%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Rating</font></small></td>
<td width="11%" bgcolor="#00FFFF"><p align="center"><small><font face="Verdana">Diary
Date</font></small></td>
<td width="76%" bgcolor="#00FFFF"><p align="left"><small><font face="Verdana">Description
of Diary Page</font></small>
</td>
<script language="php">
$query = "SELECT * FROM avg_tally ORDER BY average DESC";
$result = mysql_query($query, $mysql_link);
if(mysql_num_rows($result)) {
$rank = 1;
while($row = mysql_fetch_row($result))
{
print("</tr><tr>");
if($color == "#D8DBFE") {
$color = "#A6ACFD";
} else {
$color = "#D8DBFE";
}
print("<td width=\"6%\" bgcolor=\"$color\"><center><small>");
print("<font face=\"Verdana\">$rank</font></small></center></td>");
print("<td width=\"7%\"
bgcolor=\"$color\"><center><small>");
print("<font face=\"Verdana\"><strong>$row[1]</strong></font></small></center></td>");
print("<td width=\"11%\"
bgcolor=\"$color\"><center><small>");
$url = $row[2] . ".php3";
if(!file_exists($url)) { $url = $row[2] . ".html"; }
print("<font face=\"Verdana\"><a
href=\"$url\">$row[2]</a></font></small></center></td>");
print("<td width=\"76%\"
bgcolor=\"$color\"><left><small>");
print("<font face=\"Verdana\">$row[3]</font></small></left></td>");
$rank++;
}
}
</script>
Example #29:
Table of most useful diary entries (shown below)
Rank |
Rating |
Diary Date |
Description of Diary Page |
Looking at the source code some notes: we used an if condition to test what the color was of the last table column and change it to the other one.
Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)
[back]
07/31/00 "Sorting
and displaying results in mult-colored tables using mySQL"
[next]
PHP Diary | scriptschool.com | PHP Scripts | TD Scripts.com
Copyright 2000 php-scripts.com Last Modified 02/9/04 11:28