Amboom
Amboom

Reputation: 156

How to retain ordering asc/desc by column after reload PHP

I have page with different view (list,folder and thumb view), each view has its sort button where you can sort it by asc and desc (a-z,z-a,old-new,new-old).

Since the default sort is a-z,

What i want to do is once i click the button z-a sort in list view then change to the other view(folder view), the sort mode in folder view also the same to the last click sort mode which is z-a.

But happened on my codes is every time i reload or change my page the sort mode always in default.

if($sort == 'asc'){
        $sql = "SELECT * FROM tbl_files LEFT OUTER JOIN tbl_album_files ON tbl_files.file_id = tbl_album_files.file_id 
                                        WHERE media_type = 'video' ORDER BY file_name";
        } else if ($sort == 'desc') {
        $sql = "SELECT * FROM tbl_files LEFT OUTER JOIN tbl_album_files ON tbl_files.file_id = tbl_album_files.file_id 
                                        WHERE media_type = 'video' ORDER BY file_name DESC"; 
        } else if ($sort == 'old') {
        $sql = "SELECT * FROM tbl_files LEFT OUTER JOIN tbl_album_files ON tbl_files.file_id = tbl_album_files.file_id 
                                        WHERE media_type = 'video' ORDER BY DATE(ctime_datetime) asc, ctime_datetime asc ";
        } else if ($sort == 'new') {
        $sql = "SELECT * FROM tbl_files LEFT OUTER JOIN tbl_album_files ON tbl_files.file_id = tbl_album_files.file_id 
                                        WHERE media_type = 'video' ORDER BY DATE(ctime_datetime) desc, ctime_datetime desc ";
        }

My codes can sort well but i want is every time i reload the page or go to other page the sort will remain the same that i click before the reload. Any Idea? TIA.

Upvotes: 0

Views: 841

Answers (2)

Prathamesh Salunkhe
Prathamesh Salunkhe

Reputation: 61

You can attach a 'sort' parameter to your URL which will hold the sort-by property.

Have a look at how stackoverflow structures their URL

http://stackoverflow.com/questions/tagged/php?sort=newest&pageSize=15

Stackoverflow provides option to sort by newest, featured, votes, active, unanswered; and the URL parameter changes with each selection.

http://stackoverflow.com/questions/tagged/php?sort=featured&pageSize=15
http://stackoverflow.com/questions/tagged/php?sort=frequent&pageSize=15

Use this parameter to sort your results. In the backend you can easily access this property by doing $_GET

$_GET['sort']

By default if you want to keep ascending sort have an isset check on this parameter and if it doesn't exist sort the results in an ascending order.

if(isset($_GET['sort']) {
.....
}

Upvotes: 1

osuddeth
osuddeth

Reputation: 162

The easiest way, in my experience, is to add an argument to the url in your sort buttons. Something like mypage.php?sort=new

Then, in your PHP code, set your query up like this:

//set the bulk of the query
$mSQL="SELECT * FROM mytable ORDER BY";

//do a switch to determine what to sort by
switch($_GET['sort']){
case 'new':
$mSQL.=" mydate DESC";
break;
case 'old':
$mSQL.=" mydate ASC";
break;
case 'alpha':
$mSQL.=" namecolumn ASC";
break;
//set the default sort order in case $_GET['sort'] is not set or does not match our list
default:
$mSQL.=" sortcolumn ASC";
break;
}

//now, execute the query
$result=mysql_query($mSQL)
  or die("Bad query! ".mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {

//do something here

}

Upvotes: 0

Related Questions