Home arrow JavaScript arrow Page 2 - Sorting the Easy Way
JAVASCRIPT

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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!


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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