Ryan
Ryan

Reputation: 14659

How to use an array to order Mysql Result Set

I'm using PHP to paginate the data, but I am trying to figure how I can group the data by price, name, etc. I know I will need to use an array of some sort

SELECT PRODUCTS.name,p_id,sku, image, description, model, CATEGORIES.category
id
WHERE PRODUCTS.name LIKE  '%$title%'
ORDER BY p_id ASC $pages->limit";

Any suggestions?

Thanks

Upvotes: 0

Views: 357

Answers (1)

amosrivera
amosrivera

Reputation: 26554

No need for an array of some sort, use the same ORDER BY like this:

ORDER BY p_id, PRODUCTS.name, CATEGORIES.category, models ASC $pages->limit

UPDATE

Lets say you have a select that posts the order by, something like:

<select name="order">
      <option value="price">price</option>
      <option value="name">name</option>
</select>

Then you clean the post and put it in the query like

$order = mysql_real_escape_string($_POST["order"]);
//and then
SELECT * FROM table ORDER BY $order; 

Upvotes: 1

Related Questions