table: sortable headers

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
toffee [ Mi, 28 Juni 2006 09:25 ] [ ID #1373961 ]

Re: table: sortable headers

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='.$d irection.'">'.$f
ield.'</a>';
}

Grtz,
--
Rik Wasmus
Rik [ Mi, 28 Juni 2006 15:14 ] [ ID #1373963 ]
PHP » alt.php.sql » table: sortable headers

Vorheriges Thema: Connect to mysql?
Nächstes Thema: euro currency