thinkphp分頁+條件查詢

來源:互聯網
上載者:User

標籤:link   mit   防止   encode   bsp   查詢   不顯示   ota   data   

最近項目上面有一個帶條件查詢的分頁列表,一開始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 \Think\Page($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 \Think\Page($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();        }    }

 

thinkphp分頁+條件查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.