You are here: Re: table: sortable headers « PHP SQL « IT news, forums, messages
Re: table: sortable headers

Posted by Rik on 10/05/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.'&amp;dir='.$direction.'">'.$f
ield.'</a>';
}

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация