Jump to content

[SOLVED] Presenting 'Sort by' options


Aegist

Recommended Posts

I am trying to figure out how to provide my users the ability to sort their search results via one of numerous options.

 

What I really want is for my visitors to be able to click the title of each column, and have the page reload, sorted by that column. I've seen it many times on many different websites, and it is the most obvious method of sorting - Does anyone know of any guides/explanations for how to set up such a system? The best I can come up with involves building the current URL and adding a GET with the appropriate sort variable in it to the end of the current URL. But this seem unwieldy and overly complicated...

 

That is the short story - if that is confusing, then here is exactly what I am talking about: My current setup involves a form based "Sort by..." method

Current Drop Down sorting method

I am adding more fields to my database and more sorting options, so I really want to upgrade the sorting options to a more intuitive design, such as the one seen below, which is the current work in progress:

Sort by Up/Down Arrows next to each heading.

 

The under construction page has a (sort of) working method in place on the first column in the first table. However if you continue to click the up and down arrows, it will continue to append the get command to the end of the URL. It works, but obviously isn't the best solution.

 

Any help greatly appreciated! Complete noob here, in case you couldn't tell immediately! :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/113174-solved-presenting-sort-by-options/
Share on other sites

$_GET would be the best way

 

$col = mysql_real_escape_string($_GET['col']);

 

"SELECT * FROM table WHERE value = '$value' ORDER BY '$col' DESC"

 

or ASC for ascending results

 

Edit: I see now, I don't know why it keeps adding to the URL ???

hmmm... that is the current command being used - but the dataset is too variable for me to possibly cover every single option available. I guess the point is, that the information being "SELECT"ed and the table "WHERE" it is coming from is different in every situation, so I need to have the results previously selected by the user maintained, while allowing the "ORDER BY" to be changed repeatedly

I see now, I don't know why it keeps adding to the URL ???

I know why it does that - its how it is coded :)

 

Basically "Get self URL, add '&sortby=name ASC' to the end of URL"

So when you reload the page with  the new sortby value, if you press it again, it will just add it again.

 

 

 

I'm not familiar (at all) with the "mysql_real_escape_string" command. I'll look it up.

 

$_GET would be the best way

 

$col = mysql_real_escape_string($_GET['col']);

 

"SELECT * FROM table WHERE value = '$value' ORDER BY '$col' DESC"

 

or ASC for ascending results

 

Edit: I see now, I don't know why it keeps adding to the URL ???

SO basically, the current set up is that what is being Selected, and from which table is determined by numerous GET calls from a previous page.

ORDER BY defaults to a particular column, and I want users to be able to repeatedly change which column sorts.

 

First method is to take a snapshot of the complete current URL, assign that to $url, and stick the new $col value on to the end of that URL. This works, but results in an ever lengthening URL as long as visitors keep clicking on each "Sort By" option, because it will continue to add the $col value on the end of whatever the "current" URL is.

 

Second method is to "post" the $col variable, which would require making the arrows on the page submit buttons, and putting a form around them. This might be a cleaner method, I just need to make it all look nice enough....

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.