JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - The Basic Code


    (Page 2 of 5 )


    Let's write the basic code: it will retrieve the products from the database and display them in a table.


    <?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>';
     
    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>";
    }
     
    $output.='</table>'
     
    ? >
     
    <
    html
    <head
    </head
     
    <
    body
     
    <
    div id='content'
     
    <?=
    $output? > 
     
    </
    div
     
    </
    body
    </html>

    Ok, this snippet of code should produce this output, a table with all the products on it: 

    PHP sorting


    Make sure to check the while loop. We assign each TR element an ID.  The row’s ID corresponds to the ID number of the product (the ID number of each product is unique, so we are sure there won't be two TRs with the same ID):


    <tr id='line1'>
    <
    td>1</td>
    <
    td>Absolute Delight</td>
    <
    td>2.20</td>
    <
    td>59</td>
    <
    td>50</td>
    </
    tr>


    <tr id='line2'><td>2</td>
    <
    td>Aqua Mirabilis</td>
    <
    td>2.35</td>
    <
    td>114</td>
    <
    td>15</td>
    </
    tr>

    This table is then echoed into a DIV element, which I've assigned the ID "content". I can just hear your minds crying "Yes, I know where you want to go!"  Not really.  Ok, flip the page and let's get our hands dirty with the sorting!

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