Home arrow JavaScript arrow Page 3 - Sorting the Easy Way

Sorting the Easy Way

I bet most of us have developed an application where a list of products were retrieved from a database and shown in a table with their name, price and code. So we placed a cute little icon near each column's header that linked to the search page with a flag in the URL to let it know we wanted the results ordered by name or price. The search script kindly did its job, adding the ORDER BY clause, and the ordered results were shown to the user. Obviously this method works, but there is a problem: Why must we bother the server again to retrieve the same results that are now held in the browser, only in a different order? This article will show you the quickest way to order your database results, via JavaScript, with some work in PHP.

Author Info:
By: Alf A. Pedersen
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
March 17, 2004
  1. · Sorting the Easy Way
  2. · The Basic Code
  3. · We Love INNERHTML!
  4. · See What We've Done...
  5. · PHP Sorting

print this article

Sorting the Easy Way - We Love INNERHTML!
(Page 3 of 5 )

Right now we have a table with its rows numbered; each row's number is the ID of the product it contains. The idea of the fast sort is simple: to sort the table by, let's say, product's price we simply need to know the order of the IDs when the products list is ordered by price. So we'll sort the products list by price with PHP's array functions, and pass the IDs to a JavaScript array, which will be used to reorder the rows of the table.

Hmm… I think it'll be easier with an example:

//Connect to the database
$conn=mysql_connect('localhost','root','') or die ('Sorry, no connection to database available :-(');
//Perform our query
$query='SELECT * FROM test.products';
if (!$res=mysql_query($query$conn))
die ('Sorry, query error');
//We populate the $output variable with the HTML code to generate the results table
<tr style="font-weight:bold">
//We keep the number of results of the query, they will be used in JavaScript
while ($a=mysql_fetch_row($res))
//Append the table row to our $output variable
 $output.="<tr id='line$a[0]'> 

 //Add this product to the prices array
//We now do the sorting of the prices array, and store it in a JS array
$jsOutput='var ordPrice=new Array(';
while (list (
$key$val) = each ($prices))
.= "$key,";
//And we reverse the array to generate the inverse order JS array 
$jsOutput.='var ordPriceInv=new Array('; 
while (list (
$key$val) = each ($prices))
.= "$key,";
? >
script language="JavaScript">
num_results=<?=$num_results? >;
//We output the array definitions 
<?=$jsOutput? >
//This is the function that performs the actual sorting
function order (field)
//This var will store the row number we are grabbing from the table
 var row_number=0;

 //We must generate again the table headers
 var out='<table><tr style="font-weight:bold">
      //The field variable is the name of the array we want to use to 
      //reorder our table 
      //We travel across it and grab the content of the row that has 
      //that ID, and append it to our out variable

for (x=0;x<num_results;x++)
eval ("row_number="+field+"["+x+"];");
eval ("out+=\"<tr id='line"+row_number+"'>\"+document.getElementById('line"+row_number+"').inner
//Finally, we set the innerHTML of the content div to our new table 
div id='content'>
$output? >
br><br> <input type="button" onClick="order('ordPrice')" value="Order 
products by price"
<br><br> <input type="button" onClick="order('ordPriceInv')" value="Order 
products by price (inverse)"

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials