zoukankan      html  css  js  c++  java
  • thinkphp分页+条件查询

    最近项目上面有一个带条件查询的分页列表,一开始form用的post,点击第二页就没有跳转成功,原因是分页是get请求,post数据链接到其他页面就会被清除。

    解决办法:

    1、form表单method=get

    2、后台代码用I('get.parameterName')获取URL参数(查询条件)

    3、修改thinkphp3.2.3的Page类:

    第47行 改为$this->parameter  = empty($parameter) ? array_urlencode($_GET) : $parameter;

    array_urlencode函数(对多维数组进行urlEncode,防止GET参数中文乱码)是全局公共函数,写在Common/Common/function.php里,

    function array_urlencode($data){
        $new_data = array();
        foreach($data as $key => $val){
            // 这里我对键也进行了urlencode
            $new_data[urlencode($key)] = is_array($val) ? array_urlencode($val) : urlencode($val);
        }
        return $new_data;
    }

    至此完成了带条件查询的分页功能。

    如果读者跟我的项目一样,要对get请求进行区分,可以在html页面的form表单加入input=hidden的区分条件,条件成立表示带查询条件的get,不成立则是普通get加载页面

    给出后台代码:

    public function definedTypeList(){
            if(empty(I('get.isSearch'))){  //不是查询条件的get
                $count = M('docDefinedType')->count("d_type_id");
                $Page = new ThinkPage($count, 10);
                $Page->lastSuffix = false;//最后一页不显示为总页数
                $Page->setConfig('header','<li class="disabled hwh-page-info"><a>共<em>%TOTAL_ROW%</em>条  <em>%NOW_PAGE%</em>/%TOTAL_PAGE%页</a></li>');
                $Page->setConfig('prev','上一页');
                $Page->setConfig('next','下一页');
                $Page->setConfig('last','末页');
                $Page->setConfig('first','首页');
                $Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
                $page_show = bootstrap_page_style($Page->show());//转bootstrap样式
                $list = M('docDefinedType')->limit($Page->firstRow . ',' . $Page->listRows)
                    ->order('doc_type desc,defined_type desc')->select();
                $this->assign('definedTypeList', $list);
                if ($count <= 10) {
                    $this->assign("page", '<b>共1页</b>');
                } else {
                    $this->assign("page", $page_show);
                }
                $this->display();
            }else{
    //            header("Content-type: text/html;charset=utf-8");
                $typeCondition=$_GET['typeCondition'];
                if(!empty($typeCondition)){
                    $map['defined_type']=array('like','%'.$typeCondition.'%');
                }
                $categoryCondition=$_GET['categoryCondition'];
                if(!empty($categoryCondition)){
                    $map['doc_type']=array('like','%'.$categoryCondition.'%');
                }
                $count = M('docDefinedType')->where($map)->count("d_type_id");
                $Page = new ThinkPage($count, 10);
                $Page->lastSuffix = false;//最后一页不显示为总页数
                $Page->setConfig('header','<li class="disabled hwh-page-info"><a>共<em>%TOTAL_ROW%</em>条  <em>%NOW_PAGE%</em>/%TOTAL_PAGE%页</a></li>');
                $Page->setConfig('prev','上一页');
                $Page->setConfig('next','下一页');
                $Page->setConfig('last','末页');
                $Page->setConfig('first','首页');
                $Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
    //            p($Page->parameter);
                $page_show = bootstrap_page_style($Page->show());
    
                $list = M('docDefinedType')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)
                    ->order('doc_type desc,defined_type desc')->select();
    //            p($list);
                $this->assign('definedTypeList', $list);
                if ($count <= 10) {
                    $this->assign("page", '<b>共1页</b>');
                } else {
                    $this->assign("page", $page_show);
                }
                $searchArr['typeCondition']=$typeCondition;
                $searchArr['categoryCondition']=$categoryCondition;
                $this->assign('searchArr',$searchArr);
                $this->display();
            }
        }
  • 相关阅读:
    二进制位运算
    Leetcode 373. Find K Pairs with Smallest Sums
    priority_queue的用法
    Leetcode 110. Balanced Binary Tree
    Leetcode 104. Maximum Depth of Binary Tree
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 64. Minimum Path Sum
    Leetcode 63. Unique Paths II
    经典的递归练习
    案例:java中的基本排序
  • 原文地址:https://www.cnblogs.com/alanleung/p/6786191.html
Copyright © 2011-2022 走看看