Jump to content


Photo

Sorting table


  • Please log in to reply
7 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 21 September 2006 - 10:49 PM

Hello.

I have a table that displays my SQL query using the following 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>";
?>

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.

#2 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 21 September 2006 - 10:55 PM

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

#3 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 21 September 2006 - 10:58 PM

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.

#4 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 21 September 2006 - 11:04 PM

<?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>";
?>

test b4 u say its wrong

#5 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 21 September 2006 - 11:11 PM

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.../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.

#6 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 22 September 2006 - 07:56 AM

Anyone? :)

#7 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 23 September 2006 - 10:16 AM

bump :)

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 23 September 2006 - 11:35 AM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users