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 Quote Link to comment 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'; Quote Link to comment 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>"; } 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.