绍煞橇梧奈冷蓖坎驾桥扁渐碌
分页类的定义位于:
tp51\thinkphp\library\think\Paginator.php
分页类是抽象类,不允许实例化,只能被继承,通过实例化它的子类,
来调用抽象类的功能。
分页类中重要的方法
抽象方法 render()不能直接访问,只能通过分页类的子类来实现。在 tp51 中分页是基于 bootstrap 的,即默认的驱动(处理类)为
bootstrap
该类继承了 Paginator,所以使用的时候只需要 new Bootstrap 即可使用分页,如果需要自己写分页类,则需要将分页类写在 driver 目录,
并且继承 Paginator。
render()返回的是分页的 html 代码
数据库下载:
数据表结构:
1.配置好数据库连接
2.为表staff创建模型
<?php namespace app\index\model; use think\Model;//引入Model类 class Staff extends Model { }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use app\index\model\Staff; class Index extends \think\Controller { //分页查询 public function index() { //分页配置 $config=[ 'type'=>'bootstrap',//分页类名 'var_page'=>'page'//分页变量 ]; //每页显示的数量 $num=4; //是否是简单分页?只有:上一页,下一页 $simple=false; //用模型来获取所有的分页数据:think\Paginate //paginate()是一个普通方法,但是可以通过静态调用 $paginate=Staff::paginate($num,$simple,$config);//返回值为对象 halt($paginate); } }
执行:
修改控制器 输出分页变量
<?php namespace app\index\controller; use app\index\model\Staff; class Index extends \think\Controller { //分页查询 public function index() { //分页配置 $config=[ 'type'=>'bootstrap',//分页类名 'var_page'=>'page'//分页变量 ]; //每页显示的数量 $num=4; //是否是简单分页?只有:上一页,下一页 $simple=false; //用模型来获取所有的分页数据:think\Paginate //paginate()是一个普通方法,但是可以通过静态调用 $paginate=Staff::paginate($num,$simple,$config);//返回值为对象 $page=$paginate->render(); halt($page); } }
执行,可以看到输出是分页变量的html代码:
修改 Staff.php,将分页数据和分页变量赋值给模板,并渲染模板
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use app\index\model\Staff; class Index extends \think\Controller { //分页查询 public function index() { //分页配置 $config=[ 'type'=>'bootstrap',//分页类名 'var_page'=>'page'//分页变量 ]; //每页显示的数量 $num=4; //是否是简单分页?只有:上一页,下一页 $simple=false; //用模型来获取所有的分页数据:think\Paginate //paginate()是一个普通方法,但是可以通过静态调用 $paginate=Staff::paginate($num,$simple,$config);//返回值为对象 $page=$paginate->render(); //将分页数据赋值给模板 $this->assign('staffs',$paginate); //将分页变量赋值给模板 $this->assign('page',$page); //渲染模板 return $this->fetch(); } }
分页数据和分页变量指的是:
复制Bootstrap和jquery到public目录下
创建模板文件index.html
<!-- 导入 bootstrap 的 css 文件 --> {load href="/static/bootstrap/css/bootstrap.css"} <div class="container"> <div class="row"> <h3 class="text-center">员工信息登录表</h3> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-hover text-center"> <tr class="info"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>工资</td> <td>部门</td> </tr> {volist name="staffs" id="staff"} <tr> <td>{$staff.id}</td> <td>{$staff.name}</td> <td>{$staff.sex}</td> <td>{$staff.age}</td> <td>{$staff.salary}</td> <td>{$staff.dept}</td> </tr> {/volist} </table> </div> </div> </div> <!-- 导入 jquery --> {load href="/static/jquery/jquery-3.3.1.js"} <!-- 导入 boostrap 的 js 文件 --> {load href="/static/bootstrap/js/bootstrap.js"}
执行
加上分页变量
<!-- 导入 bootstrap 的 css 文件 --> {load href="/static/bootstrap/css/bootstrap.css"} <div class="container"> <div class="row"> <h3 class="text-center">员工信息登录表</h3> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-hover text-center"> <tr class="info"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>工资</td> <td>部门</td> </tr> {volist name="staffs" id="staff"} <tr> <td>{$staff.id}</td> <td>{$staff.name}</td> <td>{$staff.sex}</td> <td>{$staff.age}</td> <td>{$staff.salary}</td> <td>{$staff.dept}</td> </tr> {/volist} </table> {//分页变量} {$page} </div> </div> </div> <!-- 导入 jquery --> {load href="/static/jquery/jquery-3.3.1.js"} <!-- 导入 boostrap 的 js 文件 --> {load href="/static/bootstrap/js/bootstrap.js"}
执行:
在thinkPHP5.1中分页变量应该这样写:{$page|raw}
将分页条居中:
<!-- 导入 bootstrap 的 css 文件 --> {load href="/static/bootstrap/css/bootstrap.css"} <div class="container"> <div class="row"> <h3 class="text-center">员工信息登录表</h3> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-hover text-center"> <tr class="info"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>工资</td> <td>部门</td> </tr> {volist name="staffs" id="staff"} <tr> <td>{$staff.id}</td> <td>{$staff.name}</td> <td>{$staff.sex}</td> <td>{$staff.age}</td> <td>{$staff.salary}</td> <td>{$staff.dept}</td> </tr> {/volist} </table> {//分页变量} <div class="text-center">{$page}</div> </div> </div> </div> <!-- 导入 jquery --> {load href="/static/jquery/jquery-3.3.1.js"} <!-- 导入 boostrap 的 js 文件 --> {load href="/static/bootstrap/js/bootstrap.js"}
执行:
更改分页条样式:
修改 bootstrap.php
执行:
分页按照工资进行排序
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use app\index\model\Staff; class Index extends \think\Controller { //分页查询 public function index() { //分页配置 $config=[ 'type'=>'bootstrap',//分页类名 'var_page'=>'page'//分页变量 ]; //每页显示的数量 $num=4; //是否是简单分页?只有:上一页,下一页 $simple=false; //用模型来获取所有的分页数据:think\Paginate //paginate()是一个普通方法,但是可以通过静态调用 $paginate=Staff::order('salary')->paginate($num,$simple,$config);//返回值为对象 $page=$paginate->render(); //将分页数据赋值给模板 $this->assign('staffs',$paginate); //将分页变量赋值给模板 $this->assign('page',$page); //渲染模板 return $this->fetch(); } }
执行:
输出总的记录数和总的页数
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; use app\index\model\Staff; class Index extends \think\Controller { //分页查询 public function index() { //分页配置 $config=[ 'type'=>'bootstrap',//分页类名 'var_page'=>'page'//分页变量 ]; //每页显示的数量 $num=4; //是否是简单分页?只有:上一页,下一页 $simple=false; //用模型来获取所有的分页数据:think\Paginate //paginate()是一个普通方法,但是可以通过静态调用 $paginate=Staff::order('salary')->paginate($num,$simple,$config);//返回值为对象 $page=$paginate->render(); $count = $paginate->total();//总记录 $count_page=$count / $num;//总的页数 if($count % $num==0) { $count_page=(int)$count_page; } else { $count_page=(int)($count_page+1); } //将总的记录数和总的页数发送到模板 $this->view->assign('count',$count); $this->view->assign('conut_page', $count_page); //将分页数据赋值给模板 $this->assign('staffs',$paginate); //将分页变量赋值给模板 $this->assign('page',$page); //渲染模板 return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<!-- 导入 bootstrap 的 css 文件 --> {load href="/static/bootstrap/css/bootstrap.css"} <div class="container"> <div class="row"> <h3 class="text-center">员工信息登录表</h3> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-hover text-center"> <tr class="info"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>工资</td> <td>部门</td> </tr> {volist name="staffs" id="staff"} <tr> <td>{$staff.id}</td> <td>{$staff.name}</td> <td>{$staff.sex}</td> <td>{$staff.age}</td> <td>{$staff.salary}</td> <td>{$staff.dept}</td> </tr> {/volist} </table> {//输出总的记录数和总的分页数} <h2 class="text-center">数据表中共{$count}条数据,共{$conut_page}页</h2> {//分页变量} <div class="text-center">{$page}</div> </div> </div> </div> <!-- 导入 jquery --> {load href="/static/jquery/jquery-3.3.1.js"} <!-- 导入 boostrap 的 js 文件 --> {load href="/static/bootstrap/js/bootstrap.js"}
执行:
界饱涝隋稀须沁蜜撂徐惦袖边