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);
?>
Sorting by table headers
Moderator: General Moderators
Code: Select all
SELECT * FROM qb ORDER BY Pass_TDs DESCCode: Select all
SELECT * FROM qb ORDER BY Pass_TDs ASCAnyways....onto your question.
Yes, you can build it so at the top of the page, in each header, you can have somone click on something (the header itself?) and have the results Ordered by that. It is actually very simple.
Code: Select all
<?php
$linkID = @mysql_connect("localhost","","");
mysql_select_db("misc", $linkID);
$resultID = mysql_query("SELECT * FROM qb ORDER BY ".(isset($_GETї'order_by'] ? $_GETї'order_by'] : 'Pass_TDs' )." DESC", $linkID);
print "<table border=1><tr><th><a href="$PHP_SELF?order_by=position">Position</a></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);
?>You can easily just add links for the other headers in the same manner, and the IF statement handles them all equally.
Please, try and debug stuff like this on your own.
Change:
to:
Change:
Code: Select all
$resultID = mysql_query("SELECT * FROM qb ORDER BY ".(isset($_GETї'order_by'] ? $_GETї'order_by'] : 'Pass_TDs' )." DESC", $linkID);Code: Select all
$resultID = mysql_query("SELECT * FROM qb ORDER BY ".(isset($_GETї'order_by']) ? $_GETї'order_by'] : 'Pass_TDs' )." DESC", $linkID);