Jump to content

Problem with Sorting Links


perky416

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.