wwfc_barmy_army Posted September 21, 2006 Share Posted September 21, 2006 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. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted September 21, 2006 Share Posted September 21, 2006 create if commands and if that one is clicked then make a new mysql_query to get then info out and include ORDER BY ..... DESClike $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 Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 21, 2006 Author Share Posted September 21, 2006 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. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted September 21, 2006 Share Posted September 21, 2006 [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 Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 21, 2006 Author Share Posted September 21, 2006 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.htmlAnd 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. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 22, 2006 Author Share Posted September 22, 2006 Anyone? :) Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 23, 2006 Author Share Posted September 23, 2006 bump :) Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2006 Share Posted September 23, 2006 One way is to store the column name that it is sorted byWhen 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.