1. 程式人生 > >tp3.2中利用分頁類進行分頁(多條件搜尋)

tp3.2中利用分頁類進行分頁(多條件搜尋)

HTML頁面

使用者名稱:<input type="text" name="name" id="username">
方案id:<input type="text" name="" id="programid">
配資狀態:<select name="status" id="status">
    <option value="0">全部狀態</option>
 <foreach name="cap_status" item="status">
     <option value="{$status.c_s_id}">{$status.c_s_name}</option>
 </foreach>
</select>
<input type="button" value="搜尋" onclick="searcha()">

js傳值
function searcha(){
        var status=document.getElementById("status").value;
        var username=document.getElementById("username").value;
        var programid=document.getElementById("programid").value;
        $.ajax({
            url:"__APP__/Fund/Fund/fund_list",
            type:"get",
            data:{"status":status,"username":username,"programid":programid},
            success:function(e){
                $("#lista").html(e);             
            }
        })
    }

傳值到控制器並利用tp中的分頁類進行多條件搜尋並分頁
public function fund_list(){
        $status=$_GET['status'];
        $username = $_GET['username'];
        $programid = $_GET['programid'];
    	//配資狀態
		$cap_status = D("Fund")->fund_list();

        //實現分頁及搜尋
        $User = M('wcp_capital_apply'); // 例項化User物件
            $where="1=1";
            if($status){
                $where.=" and wcp_capital_status.c_s_id = '$status' ";
            }
            if($username){
                $where.=" and wcp_user.user_name='$username' ";
            }  
            if($programid){
                $where.=" and wcp_capital_apply.case_id='$programid'";
            }
        $count = $User->join("wcp_capital_status ON wcp_capital_apply.capital_status = wcp_capital_status.c_s_id")
        ->join("wcp_finace_type ON wcp_capital_apply.capital_type=wcp_finace_type.f_t_id")
        ->join("wcp_user ON wcp_capital_apply.user_id = wcp_user.user_id")
        ->field("cap_id,wcp_capital_apply.user_id,case_id,user_name,bond_price,apply_price,hailun_price,c_s_id,cordon_price,open_price,f_t_name,c_s_name,capital_use_time ")
        ->where($where)->count();// 查詢滿足要求的總記錄數
        $Page       = new \Think\Page($count,2);// 例項化分頁類 
        $show       = $Page ->show();// 分頁顯示輸出
        $list = $User->join("wcp_capital_status ON wcp_capital_apply.capital_status = wcp_capital_status.c_s_id")
        ->join("wcp_finace_type ON wcp_capital_apply.capital_type=wcp_finace_type.f_t_id")
        ->join("wcp_user ON wcp_capital_apply.user_id = wcp_user.user_id")->order("cap_id asc")
        ->field("cap_id,wcp_capital_apply.user_id,case_id,user_name,bond_price,apply_price,hailun_price,c_s_id,cordon_price,open_price,f_t_name,c_s_name,capital_use_time ")
        ->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $User->getlastsql();
        $this->assign('cap_apply',$list);// 賦值資料集
        $this->assign('page',$show);// 賦值分頁輸出
        $this->assign("cap_status", $cap_status);
        $this->display();
    }