1. 程式人生 > >jQuery頁面替換+php代碼實現搜索後分頁

jQuery頁面替換+php代碼實現搜索後分頁

tex amp == ext sta arr 頁面 判斷 sele

html代碼:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <input type="text" id="word" value="{$data.word}">
    <input type="button" value="搜索" onclick="page(1)">
<table>
    <tr>
        <
th>ID</th> <th>賬號</th> <th>密碼</th> <th>手機</th> <th>登錄時間</th> <th>登錄次數</th> <th>狀態</th> </tr> {volist name="data.list" id="v"} <tr> <td>{$v.id}</
td> <td>{$v.uname}</td> <td>{$v.pwd}</td> <td>{$v.phone}</td> <td>{$v.login_time|date="Y-m-d H:i:s",###}</td> <td>{$v.login_num}</td> <td> {switch name="$v.is_on" } {case value="1"}正常{/case} {case value="2"}鎖定{/case} {/switch}
</td> </tr> {/volist} </table> <a href="javascript:void(0);" onclick="page({$data.home_page})">首頁</a> <a href="javascript:void(0);" onclick="page({$data.prev_page})">上一頁</a> <a href="javascript:void(0);" onclick="page({$data.next_page})">下一頁</a> <a href="javascript:void(0);" onclick="page({$data.last_page})">尾頁</a> <script src="__STATIC__/js/jquery.js"></script> <script> function page(obj){ //獲取搜索框的值 var word = $("#word").val(); if(word==‘‘){ $.get("{:url(‘Three/home‘)}?page="+obj,function(data){ $("body").html(data); }) }else{ //有值 $.get("{:url(‘Three/home‘)}?page="+obj+"&word="+word,function(data){ $("body").html(data); }) } } </script> </body> </html>

php代碼:

//展示頁面
    public function home(){
        //接收關鍵字
        $word = Request::instance()->param(‘word‘);
        if(empty($word)){
            //查詢所有的數據
            //求出總條數
            $count = Db::table("user")->count();
            //設置每頁顯示的條數
            $length = 2;
            //求出來總頁數
            $zong_page = ceil($count/$length);
            //接收當前頁
            $page = Request::instance()->param(‘page‘);
            $current_page = empty($page) ? 1 : $page;
            //求出偏移量
            $limit = ($current_page-1)*$length;
            //查詢
            $data = Db::table("user")->limit($limit,$length)->select();
        }else{
            //根據關鍵字實現多條件查詢
            //求出總條數(滿足條件的)
            $count = Db::table("user")->where(‘uname|phone‘,‘like‘,"$word%")->count();
            //設置每頁顯示的條數
            $length = 2;
            //求出來總頁數
            $zong_page = ceil($count/$length);
            //接收當前頁
            $page = Request::instance()->param(‘page‘);
            $current_page = empty($page) ? 1 : $page;
            //求出偏移量
            $limit = ($current_page-1)*$length;
            //查詢
            $data = Db::table("user")->where(‘uname|phone‘,‘like‘,"$word%")->limit($limit,$length)->select();
        }
        
        //判斷頁碼
        $arr[‘list‘] = $data;
        $arr[‘home_page‘] = 1;
        $arr[‘prev_page‘] = $current_page-1 <= 1 ? 1 : $current_page-1;
        $arr[‘next_page‘] = $current_page+1 >= $zong_page ? $zong_page : $current_page+1;
        $arr[‘last_page‘] = $zong_page;
        $arr[‘word‘] = empty($word) ? null : $word;

        return view(‘home‘,[‘data‘=>$arr]);
    }

jQuery頁面替換+php代碼實現搜索後分頁