Jump to content

Sorting easily in ascending and descending?


rockinaway

Recommended Posts

Firstly, you'll need a link like: <a href='yourpage.php?sort=asc'>Sort Asc</a> and similiarly, <a href='yourpage.php?sort=desc'>Sort Desc</a>

Then, on your page, use GET to retrieve the sort method:

[code]
<?php
if(isset($_GET['sort']){
$sort = mysql_real_escape_string($_GET['sort']);
$sql = "SELECT * FROM `yourtable` WHERE.... ORDER BY `yourfieldname` '$sort'");
mysql_query($sql) or die(mysql_error());
}else{
//perfrom the query without sorting
}
//show the data
?>
[/code]

Obviously its just a sample, but without any of your code i cant really help you further.
Do you mean that you want to be able to sort one of 4 columns? In which case, just modify it a little. Make the links like:
<a href='yourpage.php?sort=asc&field=field1'>Sort By Field 1 Asc</a>

then the code:

[code]
<?php
if(isset($_GET['sort'] && isset($_GET['field']){
$sort = mysql_real_escape_string($_GET['sort']);
$field = mysql_real_escape_string($_GET['field']);
$sql = "SELECT * FROM `yourtable` WHERE.... ORDER BY `$field` '$sort'");
mysql_query($sql) or die(mysql_error());
}else{
//perfrom the query without sorting
}
//show the data
?>
[/code]

I dont think you're gonna get shorter for sorting data. And its not that long anyway is it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.