控制器代码:
public function userLists(){ $model = new User(); $word = input('get.word'); //接收当前页 $page = input('get.page'); $cpage = empty($page) ? 1 : $page; //设置每页显示的条数 $length = 3; //获取总条数 if(empty($word)){ $count = $model->getCount(); }else{ $count = $model->getCount2($word); } //求出总页数 $num_page = ceil($count/$length); //偏移量 $limit = ($cpage-1)*$length; //查询 if(empty($word)){ $data = $model->getLists($limit,$length); }else{ $data = $model->getLists2($limit,$length,$word); } $arr['word'] = empty($word) ? "" : $word; $arr['home'] = 1; $arr['prev'] = $cpage-1<=1 ? 1 : $cpage-1; $arr['next'] = $cpage+1>=$num_page ? $num_page : $cpage+1; $arr['last'] = $num_page; $arr['list'] = $data; return view('userLists',['arr'=>$arr]); }
模型层代码:
//总条数 public function getCount(){ return $this->count(); } public function getCount2($word){ return $this->where('username','like',"%$word%")->count(); } //分页查询 public function getLists($limit,$length){ return $this->limit($limit,$length)->select(); } public function getLists2($limit,$length,$word){ return $this->where('username','like',"%$word%")->limit($limit,$length)->select(); }
静态页代码:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <input type="text" name="word" value="{$arr.word}"> <input type="button" value="搜索" onclick="page(1)"> <table border="1"> <tr> <td>主键</td> <td>用户名</td> <td>手机</td> <td>邮箱</td> <td>地址</td> </tr> {volist name="arr.list" id="v"} <tr> <td>{$v.id}</td> <td>{$v.username}</td> <td>{$v.tel}</td> <td>{$v.email}</td> <td>{$v.address}</td> </tr> {/volist} </table> <a href="javascript:void(0)" onclick="page({$arr.home})">首页</a> <a href="javascript:void(0)" onclick="page({$arr.prev})">上一页</a> <a href="javascript:void(0)" onclick="page({$arr.next})">下一页</a> <a href="javascript:void(0)" onclick="page({$arr.last})">尾页</a> </body> </html> <script src="__STATIC__/js/jq.js"></script> <script> function page(obj) { var word = $("input[name='word']").val(); $.get("{:url('Testone/userLists')}?page="+obj+"&word="+word,function (data) { $("body").html(data); }) } </script>