Sorting by table headers
Posted: Tue May 14, 2002 6:12 am
peoples,
I am new to PHP and sql so be kind. I have a php script that displays my data from a database but I want to sort it by the column header (i.e. the column header would have a link that would sort the table by that column).
Is it possible to do this all on one page or do I need to build seperate pages for the different columns? I also could not get the ORDER BY function to work, it did not recognize that they numbers were double digits (i.e. 8 was at the top and 36 near the bottom because of sorting on the first nuimber only). Could you look at this and tell me what I am doing wrong? Thanks
George
<?php
$linkID = @mysql_connect("localhost","","");
mysql_select_db("misc", $linkID);
$resultID = mysql_query("SELECT * FROM qb ORDER BY Pass_TDs DESC", $linkID);
print "<table border=1><tr><th>Position</th>";
print "<th>Name</th><th>Team</th>";
print "<th>Pass Comp</th><th>Pass Att</th>";
print "<th>Pass Yds</th><th>Pass TDs</th>";
print "<th>Int</th><th>Rush Att</th><th>Rush Yds</th><th>Rush TDs</th></tr>";
while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($linkID);
?>
I am new to PHP and sql so be kind. I have a php script that displays my data from a database but I want to sort it by the column header (i.e. the column header would have a link that would sort the table by that column).
Is it possible to do this all on one page or do I need to build seperate pages for the different columns? I also could not get the ORDER BY function to work, it did not recognize that they numbers were double digits (i.e. 8 was at the top and 36 near the bottom because of sorting on the first nuimber only). Could you look at this and tell me what I am doing wrong? Thanks
George
<?php
$linkID = @mysql_connect("localhost","","");
mysql_select_db("misc", $linkID);
$resultID = mysql_query("SELECT * FROM qb ORDER BY Pass_TDs DESC", $linkID);
print "<table border=1><tr><th>Position</th>";
print "<th>Name</th><th>Team</th>";
print "<th>Pass Comp</th><th>Pass Att</th>";
print "<th>Pass Yds</th><th>Pass TDs</th>";
print "<th>Int</th><th>Rush Att</th><th>Rush Yds</th><th>Rush TDs</th></tr>";
while ($row = mysql_fetch_row($resultID))
{
print "<tr>";
foreach ($row as $field)
{
print "<td>$field</td>";
}
print "</tr>";
}
print "</table>";
mysql_close($linkID);
?>