perky416 Posted April 1, 2011 Share Posted April 1, 2011 Hi Everyone, I have a page on my website that displays a list of products from a mysql database in a table. Currently when the column titles are clicked, the data is sorted in order with the highest first. $sort = $_GET['sort']; if ($sort == 'price') { $query = mysql_query("SELECT * FROM products ORDER BY price DESC"); } else{ $query = mysql_query("SELECT * FROM products ORDER BY product"); } ... echo "<a href='products.php?sort=price'>Price</a>"; Does anybody know how i would go about setting it up so that if the sort link is clicked a second time, the data is re ordered from descending to ascending? Then if the click is clicked again it would change back to descending, and so on.....? Many thanks Link to comment https://forums.phpfreaks.com/topic/232437-problem-with-sorting-links/ Share on other sites More sharing options...
lastkarrde Posted April 1, 2011 Share Posted April 1, 2011 Perhaps not the neatest way, but you could save the state using sessions. if(isset($_SESSION['order']) && $_SESSION['order'] == 'DESC') { $_SESSION['order'] == 'ASC'; } $_SESSION['order'] = 'DESC'; Link to comment https://forums.phpfreaks.com/topic/232437-problem-with-sorting-links/#findComment-1195645 Share on other sites More sharing options...
perky416 Posted April 1, 2011 Author Share Posted April 1, 2011 Hi mate thanks for your response, I managed to figure out how to do it using the following: echo "<td><a href='products.php"; if ($sort == 'price_asc') { echo "?sort=price_desc'>Price</td>"; } else { echo "?sort=price_asc'>Price</td>"; } Link to comment https://forums.phpfreaks.com/topic/232437-problem-with-sorting-links/#findComment-1195657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.