Jump to content

Change PHP search query ORDER BY


kanala

Recommended Posts

How do I change the ORDER BY of a search query?Example

$query = "SELECT * FROM products WHERE Item_name LIKE $ItemName ORDER BY Item_name";
When clicking a link, how do I change it so that it changes to ORDER BY Price?
Link to comment
Share on other sites

in html create links like this

Order by: <a href='?orderby=Item_name'>Item name</a> <a href='?orderby=Price'>Price</a>

then modify the php code

$query = "SELECT * FROM products WHERE Item_name LIKE $ItemName ORDER BY ".$_GET['orderby'];

and make sure to check the $_GET for any dangerous character before passing it to the query

Link to comment
Share on other sites

and make sure to check the $_GET for any dangerous character before passing it to the query
E.g. with mysql_real_escape_string() or with a Regex such as /$[a-z]^/i
Link to comment
Share on other sites

Thanks, but when I do order by Price, it does not actually order them but sorts them out into the prices e.g. £12.99 £12.99 £10.99 £10.99 £15.99 £15.99, they are not in order from low to high. How would I make it order to low to high? Note, the numbers do not include the £ sign, e.g. 12.99.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...