|
Posted by Rik on 10/14/41 11:51
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
[Back to original message]
|