Jump to content

Sorting table


wwfc_barmy_army

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.
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.