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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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