douyin2435 2015-08-27 05:35
浏览 48

重装PHP后如何保持按列排序asc / desc

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.

  • 写回答

2条回答 默认 最新

  • dounanyin3179 2015-08-27 05:44
    关注

    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
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)