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

How to Unlock a feature in iPhone app with a review given by user in AppStore

iphone app-store review

I have developed an app for GMAT exam, My first exam paper is Locked.. I want to Unlock the paper whenever the user gives a review for my app in appstore. Suggest me any tutorials to do that..? Whe...

Generating a multipage tiff is not working

c# gdi+ tiff multipage

I'm trying to generate a multipage TIFF file from an existing picture using code by Bob Powell:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.