Cant really find any useful information on this through Google so hope someone here with some knowledge can help.
I have a set of results which are pulled from a multi dimensional array. Currently the array key is the price of a product whilst the item contains another array which contains all the product details.
key=>Item(name=>test, foo=>bar)
So currently when I list the items I just order by the key, smallest first and it lists the products smallest price first.
However I want to build on this so that when a user sees the results they can choose other ordering options like list all products by a name, certain manufacturer, colour, x ,y ,z etc etc from a drop down box(or something similar)
This is where I need some guidance. Im just not sure how to go about it or best practise or anything. The only way I can think of is to order all the items by the nested array eg by the name, manufacturer etc. but how do I do that in PHP?
Hope you understand what im trying to achieve(if not just ask). Any help on this with ideas, approaches or examples would be great.
Thanks for reading
p.s Im using PHP5
Updated example with a little snippet using in memory database 🙂
P.S: the XSS protection in this example is not needed at all because I check the input as boolean value. To see results in reverse order you specify
order?descFirst I have a couple of questions you are saying that you are using PHP5. How do you retrieve your data(RDBMS)? If not, PHP5 has SQLite enabled by default. I think you should be using at least a RDBMS(SQLite/etc) to do the heavy lifting for you.
When you learn SQL you don’t have to any sorting in PHP. I think this PDO tutorial while give you insides how to use SQL while doing it safely. SQL is vulnerable to SQL-injections but thanks to PDO’s prepared statements you don’t have to worry about that anymore.
Use ORDER BY to order. I would use a datatable to do the sorting client-side. Also safes you to do work on the server(PHP). You could for example look at YUI2’s datatable.