0

This may be a bit much to ask here, but what I want are some simple directions or examples of a PHP sorting script, so that I can work it out myself.

I have a webshop where I want my customers to sort their products, bases on some variables such as the price, or color of the product.

I'm building a menu on the left of my site with checkboxes where people can check the variable they want to sort with. When they check a box, the page container must refresh and only the products that match the requirements must be visible. See http://amzn.to/ryeIjF for an example of what I want to achieve.

Note that all the properties of my products are stored in a mysql database.

Forza
  • 1,517
  • 2
  • 23
  • 47

5 Answers5

3

Sorting multi-dimensional arrays in PHP is actually not too difficult... I assume that's what you're looking to do (sort by price, popularity, most recent, etc..).

Assume the following structure for a multi-dimensional array $itemsArray

{[
    {
      "id" : "2643",
      "name" : "Leather Jacket",
      "price" : "249.99"
    },
    {
      "id" : "2645",
      "name" : "Suede Jacket",
      "price" : "289.99"
    },
    ...
]}

This can be sorted like...

usort($itemsArray, 'sortItemsByPrice');

function sortItemsByPrice($objA, $objB) {
    //This function returns a -1, 0, or 1 depending on the order  
    //of object A and Object B

    $aVal=$objA['price'];
    $bVal=$objB['price'];
    if ($a == $b) {  
        //or maybe sort on a secondary field
        return 0;    
    }
    return ($a < $b) ? 1 : -1;      
} 

HOWEVER

If you can sort directly in SQL, do so using the ORDER BY keywords in your SQL statement

//Order by price, lowest->highest
SELECT id,name,price FROM products ORDER BY price

//Order by price, highest->lowest
SELECT id,name,price FROM products ORDER BY price desc
Dutchie432
  • 27,738
  • 20
  • 88
  • 109
  • This is indeed kind of what I'm looking for. Within a few hours I will try to implement this on our site, and see how to make the checkboxes call the function :) – Forza Dec 21 '11 at 19:15
  • I have to re-state this. For the type of functionality you are looking to perform, it is probably best to do the sorting on the SQL side. It will yield MUCH faster results. – Dutchie432 Dec 22 '11 at 10:11
2

If your data is stored in MySQL database you can simply sort them when you are querying database. http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html http://php.net/manual/en/function.sort.php I think that better is to, if is possible, deal with data at their source.

Emran
  • 943
  • 12
  • 24
1

PHP shouldn't be doing the sorting for you. YOu can just make a request to your php script build a SQL query dynamically filter the values selected and have your db do the sorting for you! this is what it is made and optimized to do!!!

dm03514
  • 50,477
  • 16
  • 96
  • 131
  • I won't down vote, because in most cases this is true. But, in high traffic sites it is often a requirement that application logic is used for this exact type of thing in order to offload traffic from the database. – sberry Dec 21 '11 at 19:10
0

Take a look at: http://php.net/manual/en/array.sorting.php

Jeremy
  • 2,325
  • 6
  • 23
  • 23
0

Use Order By clause in SQL query like this ORDER BY products or you can sort later by first retrieving result set in array using mysql_fetch_array($result) method

Bhavik Patel
  • 25
  • 1
  • 5