PHP分页数据和排序

<?php
    function filterStrGet(){
        $a = explode('&', $_SERVER["REQUEST_URI"]);
        $i = 0;
        while($i < count($a)){
            $b = explode('=', $a[$i]);
            if(htmlspecialchars(urldecode($b[0])) == "filter"){
                $filter = urldecode($b[1]);
                return $filter;
            }
            $i++;
        }
    }
    $conn = mysql_connect('localhost', 'changweihua', 'changweihua');
    mysql_select_db("sencha");
    mysql_query("SET NAMES UTF8");
       
    $start = ($_REQUEST['start'] != '') ? $_REQUEST['start'] : 0;
    $limit = ($_REQUEST['limit'] != '') ? $_REQUEST['limit'] : 10;
    $sort = ($_REQUEST['sort'] != null) ? $_REQUEST['sort'] : '';
       
    if($sort != ''){
        $sort_str = str_replace('\\', '', $sort);
        $sort_json = json_decode($sort_str);
    }
       
    $sql = 'select * from tbBookInfo';
       
    $filter = filterStrGet();
    if($filter != ''){
        $filter_json = json_decode($filter, true);
        $field = $filter_json[0]["property"];
        $value = $filter_json[0]["value"];
        $sql = $sql." where ".$field." like '%".$value."%'";
        //echo $sql;
    }
       
    $result = mysql_query($sql);
       
    if(!$result){
        echo '{"success":false, "message": "读取数据失败1"}';
        exit();
    }
       
       
    $count = 0;
       
    if(mysql_num_rows($result) > 0){
        $count = mysql_num_rows($result);
           
        if($sort != ''){
            $field = $sort_json[0]->property;
            $direction = $sort_json[0]->direction;
            $sql2 = $sql." order by ".$field." ".$direction;
        }else{
            $sql2 = $sql;
        }
           
        $sql2 = $sql2.' limit '.$start.' , '.$limit;
        $result = mysql_query($sql2);
           
        if(!$result){
            die('{"success":false, "message": "读取数据失败2"}');
        }
           
        $arr = array();
           
        if(mysql_num_rows($result) > 0){
            while ($obj = mysql_fetch_object($result)) {
                $arr[] = $obj;
            }
            echo '{"success": true, "bookCount":'.$count.', "books":'.json_encode($arr).'}';
        }else{
            echo '{"success": false, "message": "读取数据失败3", "books":""}';
        }
    }else{
        echo '{"success": false, "message": "读取数据失败4", "books":""}';
    }
       
?>


知识共享许可协议
《PHP分页数据和排序》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     152.92ms