JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - 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:


    <?php 
    //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
    $output='<table>
    <tr style="font-weight:bold">
    <td>ID</td>
    <td>NAME</td>
    <td>PRICE</td>
    <td>CODE</td>
    <td>WEIGHT</td></tr>';
     
    //We keep the number of results of the query, they will be used in JavaScript
    $num_results=mysql_num_rows($res);
     
    while ($a=mysql_fetch_row($res))
    {
     
    //Append the table row to our $output variable
     $output.="<tr id='line$a[0]'> 
    <td>$a[0]</td>
    <td>$a[1]</td>
    <td>$a[2]</td>
    <td>$a[3]</td>
    <td>$a[4]</td>
    </tr>";


     //Add this product to the prices array
     $prices[$a[0]]=$a[2];
    }
     
    $output.='</table>';
     
    //We now do the sorting of the prices array, and store it in a JS array
    $jsOutput='var ordPrice=new Array(';
     
    asort($pricesSORT_NUMERIC); 
    reset 
    ($prices);
     
    while (list (
    $key$val) = each ($prices))
    {
     $jsOutput
    .= "$key,";
    }
     
    $jsOutput=substr($jsOutput,0,-1); 
    $jsOutput
    .=");rn";
     
    //And we reverse the array to generate the inverse order JS array 
    $jsOutput.='var ordPriceInv=new Array('; 
     
    $prices=array_reverse($pricesTRUE);
    reset 
    ($prices);
     
    while (list (
    $key$val) = each ($prices))
    {
     $jsOutput
    .= "$key,";
    }
     
    $jsOutput=substr($jsOutput,0,-1); 
    $jsOutput
    .=");rn";
    ? >
     
    <
    html
    <head>
     
    <
    script language="JavaScript">
     
    var 
    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">
    <td>ID</td>
    <td>NAME</td>
    <td>PRICE</td>
    <td>CODE</td>
    <td>WEIGHT</td>
    </tr>';
     
          //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
     
    HTML+'</tr>';"
    );
     
    }
     out
    +='</table>';
     
    //Finally, we set the innerHTML of the content div to our new table 
     document.getElementById('content').innerHTML=out;
    }
    </script> 
     
    </head>
    <body>
     
    <
    div id='content'>
    <?=
    $output? >
    </
    div>
     
    <
    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)"
    >
    </
    body
    </html>

    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 6 hosted by Hostway
    Stay green...Green IT