Home arrow PHP arrow Page 3 - Ordering Columns in DDBB Search Results

The Basic Code - PHP

This article will show you the fastest way to order your database results, via JavaScript (with some work in PHP). It adds some more work to your script, but once you see the results you won't use the approach of adding the ORDER By clause anymore.

TABLE OF CONTENTS:
  1. Ordering Columns in DDBB Search Results
  2. The Test Scenario
  3. The Basic Code
  4. We Love InnerHTML!
  5. I Need More!
  6. The Final Words
By: Alvaro Calleja
Rating: starstarstarstarstar / 13
December 06, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

<?

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

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


Ordering Columns in DDBB Search Results


Make sure you check the while loop. We assign each TR element an id, being this id the ID number of the product (the ID number of each product is unique, so we are sure there won't be two TR 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 hear you saying "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 PHP Articles          >>> More By Alvaro Calleja
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: