For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > June 2006 > table: sortable headers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author table: sortable headers
toffee

2006-06-28, 3:58 am

Hi all,

i have a simple table which outputs the results of a select query. how can i
make the headers sortable so that if you click on them, the values are
sorted ASC if its already DESC and vice versa.

appreciate all suggestions

kind regards


Rik

2006-06-28, 8:00 am

toffee wrote:
> Hi all,
>
> i have a simple table which outputs the results of a select query.
> how can i make the headers sortable so that if you click on them, the
> values are sorted ASC if its already DESC and vice versa.
>
> appreciate all suggestions


Perhaps users are more happy with a javascript solution as this prevents
pagereloading.

Possible in PHP (excuse the somewhat messy code...):

$fields = ('name_of_field_1','name_of_field2", etc...);
$order = '';
$dir = 'ASC';
mysql_real_escape_string(
if(isset($_GET['order']) isset($_GET['dir'])
$_GET['order'] = mysql_real_escape_string($_GET['order'])
;
$_GET['dir'] = mysql_real_escape_string($_GET['dir']);
if(in_array($_GET['order'],$fields) && ($_GET['dir']=='ASC' ||
$_GET['dir']=='DESC')){
$dir = ($_GET['dir']=='ASC') ? 'DESC' : 'ASC';
$order = ' ORDER BY `'.$_GET['order'].'` '.$_GET['dir'];
}
}
$query = 'Some query'.$order;

//th_row, assuming you have a list of fields:
foreach($fields as $field){
$direction = ($field==$_GET['order'])? $dir : 'ASC';
$echo = '<a
href="'.$_SERVER['PHP_SELF'].'?order='.$field.'&dir='.$direction.'">'.$f
ield.'</a>';
}

Grtz,
--
Rik Wasmus


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com