Live sort query results in php

I have a query which goes through the table and outputs the data like this:


$query = mysql_query("SELECT * FROM `products`");
while($row = mysql_fetch_assoc($query)){
    $array[] = $row;

foreach ($array as $key) {
    $name[]    = $key['name'];
    $desc[]    = $key['desc'];
    $cost[]    = $key['price'];

$c = 0;
    echo 'Name: '.$name[$c].'<br>';
    echo 'Desc: '.$desc[$c].'<br>';
    echo 'Price: '.$cost[$c].'<br><br>';


Name: xyz1 Desc: asdxsadasda Price: 999

Name: xyz2 Desc: asdxsadasda Price: 333

Name: xyz3 Desc: asdxsadasda Price: 666

I want to be able to sort these results based on the price of each item. Can I output the result in a jSON file and use that to sort the results (live-without loading the page)?

Could you suggest me a better way to sort the results without having to load the page?


If you MUST sort the array in PHP, after you got the query result, use the array_multisort function in PHP (

However, if your life does not depend on sorting in PHP, sort at the source in your SQL (the way God intended it to be) it's going to be much much faster and it will take the load off the web server.

Hope this helps.

$query = mysql_query("SELECT * FROM `products` WHERE `category` order by products.price,");

You choose sort by ASC or DESC

Need Your Help

apple's appstore : while updating my app from appstore, it installs as separate application.What is the reason for that?


i've developed an application and posted in apple's appstore. Now i've modified some features in it. Posted the updates in appstore. The appstore approved my updates. Now when i try to update my a...