Jump to content

Archived

This topic is now archived and is closed to further replies.

wwfc_barmy_army

Sorting table

Recommended Posts

Hello.

I have a table that displays my SQL query using the following code:
[code]<?php
$result = mysql_query('SELECT name, sitelink, sitedownload, dateadded, publisher FROM site');
$qry = mysql_fetch_array($result);

print "<table border=1 class=list><tr><td>Site Name</td><td>Site Link</td><td>Site Download</td><td>Date Added</td><td>Publisher</td></tr><tr>";

while ($qry = mysql_fetch_array($result))
{
print "<tr>";
print "<td>$qry[name]</td>";
print "<td><a href=$qry[sitelink]>$qry[sitelink]</a></td>";
print "<td><a href=$qry[sitedownload]>$qry[sitedownload]</a></td>";
print "<td>$qry[dateadded]</td>";
print "<td>$qry[publisher]</td>";
print "</tr>";
}

print "</table>";
?>[/code]

How can i make it so for example if you click on the table header 'site name' it lists in alphabetical order, but if you click on date added (written like 2006-09-20) it shows the newest one at the top?

Can anyone offer any advice?

Thanks, you guys are ace!

Peter.

Share this post


Link to post
Share on other sites
create if commands and if that one is clicked then make a new mysql_query to get then info out and include

ORDER BY ..... DESC

like

$result = mysql_query('SELECT name, sitelink, sitedownload, dateadded, publisher FROM site ORDER BY dateadded DESC');

i have one question is name sitelink sitedownload dateadded and publisher all the info in the db site because if so then all u have to do is

$result = mysql_query('SELECT * FROM site ORDER BY dateadded DESC');

* means all

Share this post


Link to post
Share on other sites
No, it's all different information, i had a search and found out how to order them, but i can't find out how to make the table header into a link that sorts that column in the relevant order. Any ideas?

Cheers.

Pete.

Share this post


Link to post
Share on other sites
[code]<?php
$result = mysql_query('SELECT name, sitelink, sitedownload, dateadded, publisher FROM site ORDER BY name DESC');


print "<table border=1 class=list>";
echo("<thead><tr><td>Site Name</td><td>Site Link</td><td>Site Download</td><td>Date Added</td><td>Publisher</td></tr></thead><tbody>");
while ($qry = mysql_fetch_array($result))
{
print "<tr>";
print "<td>$qry[name]</td>";
print "<td><a href=$qry[sitelink]>$qry[sitelink]</a></td>";
print "<td><a href=$qry[sitedownload]>$qry[sitedownload]</a></td>";
print "<td>$qry[dateadded]</td>";
print "<td>$qry[publisher]</td>";
print "</tr>";
}

print "</tbody></table>";
?>[/code]

test b4 u say its wrong

Share this post


Link to post
Share on other sites
ok thanks. From what i make out that will make it so it lists 'name' in descending order. Although i'm trying to find out how i can make it so when i click on one of the table headers (eg. Name, date added) it sorts that column in either asending or decending order?

For example if you go here:
http://www.phpfreaks.com/forums/index.php/board,1.0.html
And click on one of the headers (eg. subject, started by) it will sort them in order. How can i do this with my table?

Thanks.

Peter.

Share this post


Link to post
Share on other sites
One way is to store the column name that it is sorted by

When a column head is clicked check if is same as stored name. If it's the same column, toggle the ASC/DESC setting. If it's a different column, set ASC sort by default.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.