ThinkPHP 分页详解及分页应用实例
数据分页是 Web 开发中一个常见的功能,ThinkPHP 内置了分页类(ThinkPHP 系统目录下 Lib/ORG/Util/Page.class.php),可以直接使用.
分页类语法: Page(totalRows, listRows, parameter)
参数 说明
totalRows 必选,总的记录数
parameter 可选,分页跳转的参数
分页例子
分页操作
在查询操作中,使用 import 指令导入分页类,一个应用实例如下:
public function select(){ $Dao = M( "User" ); // 计算总数 $count = $Dao->count(); // 导入分页类 import("ORG.Util.Page"); // 实例化分页类 $p = new Page($count, 10); // 分页显示输出 $page = $p->show(); // 当前页数据查询 $list = $Dao ->order( 'uid ASC' )->limit( $p ->firstRow. ',' . $p ->listRows)->select(); // 赋值赋值 $this->assign('page', $page); $this ->assign( 'list' , $list ); $this ->display(); }语法说明: Page 类需要两个初始化参数:数据总数和每页显示的数据数,这也是分页的基本原理.
实例化一个分页类后,调用 show() 方法显示输出分页代码,在查询当前页面显示数据是,使用了 limit 方法,注意参数要使用 Page 类的属性.
分页模板: 操作对应的模板为 select.html,参考代码(只列出关键部分)如下:
< table border = "1" > < tr > < th width = "10%" > ID </ th > < th width = "30%" > 用户名 </ th > < th width = "30%" > 电子邮件 </ th > < th > 注册时间 </ th > </ tr > < volist name = "list" id = "vo" > < tr > < td align = "center" > {$vo['uid']} </ td > < td > {$vo['username']} </ td > < td > {$vo['email']} </ td > < td > {$vo['regdate']| date = "Y-m-d H:i" ,###} </ td > </ tr > </ volist > </ table > < div > {$page} </ div >模板中将查出的用户数据以表格的形式列出,在表格底部输出分页代码,效果如下:
58 条记录 2/6 页 上一页 下一页 1 2 3 4 5 下5页 最后一页
查看更多关于ThinkPHP 分页详解及分页应用实例 - Thinkphp的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did6364