JavaScript
  Home arrow JavaScript arrow Sorting the Easy Way
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Sorting the Easy Way
By: Alf A. Pedersen
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-03-17

    Table of Contents:
  • Sorting the Easy Way
  • The Basic Code
  • We Love INNERHTML!
  • See What We've Done...
  • PHP Sorting

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Sorting the Easy Way


    (Page 1 of 5 )

    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.

    The Test Scenario

    First of all, we'll create a products table in our MySQL test database and insert some records into it:


    CREATE TABLE products (
      
    ID smallint(5unsigned NOT NULL auto_increment,
      NAME varchar
    (125NOT NULL default '',
      PRICE float
    (8,2NOT NULL default '0.00',
      CODE varchar
    (10NOT NULL default '',
      WEIGHT smallint
    (5unsigned NOT NULL default '0',
      PRIMARY KEY 
    (ID)
      
    TYPE=MyISAM;
    INSERT INTO products VALUES (1'Absolute Delight''2.20''59'50);
    INSERT INTO products VALUES 
    (2'Aqua Mirabilis''2.35''114'15);
    INSERT INTO products VALUES 
    (3'Back For Breakfast''8.30''1409'500);
    INSERT INTO products VALUES 
    (4'Breeze on a Sea Air''5.95''223'75);
    INSERT INTO products VALUES 
    (5'Figs and Leaves''3.90''22819'100);
    INSERT INTO products VALUES 
    (6'I love Juicy''5.95''2006'250);
    INSERT INTO products VALUES 
    (7'Ocean Salt''9.75''2128'150);
    INSERT INTO products VALUES 
    (8'Serenity''49.95''2083'350);
    INSERT INTO products VALUES 
    (9'Tisty Tosty''2.35''5'60);
    INSERT INTO products VALUES 
    (10'Waving Not Drowning''1.95''15'40);



    If nothing went wrong we should have our products table with 10 records. Roll up your sleeves; we are now ready to jump into the actual code!


    More JavaScript Articles
    More By Alf A. Pedersen


     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT