TP3.2框架手册,有一个数据分页,不过每次都要写太多的代码,还有中文设置等有些麻烦,做为程序开发者,有必要整理下:
O、先看效果图
一、分页方法
<span>/*</span><span>* * TODO 基础分页的相同代码封装,使前台的代码更少 * @param $m 模型,引用传递 * @param $where 查询条件 * @param int $pagesize 每页查询条数 * @return \Think\Page </span><span>*/</span> <span>function</span> getpage(&<span>$m</span>,<span>$where</span>,<span>$pagesize</span>=10<span>){ </span><span>$m1</span>=<span>clone</span> <span>$m</span>;<span>//</span><span>浅复制一个模型</span> <span>$count</span> = <span>$m</span>->where(<span>$where</span>)-><span>count</span>();<span>//</span><span>连惯操作后会对join等操作进行重置</span> <span>$m</span>=<span>$m1</span>;<span>//</span><span>为保持在为定的连惯操作,浅复制一个模型</span> <span>$p</span>=<span>new</span> Think\Page(<span>$count</span>,<span>$pagesize</span><span>); </span><span>$p</span>->lastSuffix=<span>false</span><span>; </span><span>$p</span>->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录 每页<b>%LIST_ROW%</b>条 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>'<span>); </span><span>$p</span>->setConfig('prev','上一页'<span>); </span><span>$p</span>->setConfig('next','下一页'<span>); </span><span>$p</span>->setConfig('last','末页'<span>); </span><span>$p</span>->setConfig('first','首页'<span>); </span><span>$p</span>->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'<span>); </span><span>$p</span>->parameter=I('get.'<span>); </span><span>$m</span>->limit(<span>$p</span>->firstRow,<span>$p</span>-><span>listRows); </span><span>return</span> <span>$p</span><span>; }</span>
getpage方法可以放在TP框架的 Application/Common/Common/function.php,这个文档可以专门放置一些通用的方法,在哪里都可以调用(如:Controller文件,View文件等)。
二、调用分页方法
<span>$m</span>=M('products'<span>); </span><span>$p</span>=getpage(<span>$m</span>,<span>$where</span>,10<span>); </span><span>$list</span>=<span>$m</span>->field(<span>true</span>)->where(<span>$where</span>)->order('id desc')-><span>select(); </span><span>$this</span>-><span>list</span>=<span>$list</span><span>; </span><span>$this</span>->page=<span>$p</span>->show();
再是View代码
<div class="pagination"><span> {$page} </div></span>
三、最后就是分页的样式了,这个有些乱,因后台框架网上下载的,样式还没来的及整理,这个样式也可以自己实现,简单的。
<span>.pagination ul </span>{<span> display</span>:<span> inline-block</span>;<span> margin-bottom</span>:<span> 0</span>;<span> margin-left</span>:<span> 0</span>;<span> -webkit-border-radius</span>:<span> 3px</span>;<span> -moz-border-radius</span>:<span> 3px</span>;<span> border-radius</span>:<span> 3px</span>;<span> -webkit-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> -moz-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>; }<span> .pagination ul li </span>{<span> display</span>:<span> inline</span>; }<span> .pagination ul li.rows </span>{<span> line-height</span>:<span> 30px</span>;<span> padding-left</span>:<span> 5px</span>; }<span> .pagination ul li.rows b</span>{<span>color</span>:<span> #f00</span>}<span> .pagination ul li a, .pagination ul li span </span>{<span> float</span>:<span> left</span>;<span> padding</span>:<span> 4px 12px</span>;<span> line-height</span>:<span> 20px</span>;<span> text-decoration</span>:<span> none</span>;<span> background-color</span>:<span> #fff</span>;<span> background</span>:<span> url('../images/bottom_bg.png') 0px 0px</span>;<span> border</span>:<span> 1px solid #d3dbde</span>; <span>/*</span><span>border-left-width: 0;</span><span>*/</span><span> margin-left</span>:<span> 2px</span>;<span> color</span>:<span> #08c</span>; }<span> .pagination ul li a:hover</span>{<span> color</span>:<span> red</span>;<span> background</span>:<span> #0088cc</span>; }<span> .pagination ul li.first-child a, .pagination ul li.first-child span </span>{<span> border-left-width</span>:<span> 1px</span>;<span> -webkit-border-bottom-left-radius</span>:<span> 3px</span>;<span> border-bottom-left-radius</span>:<span> 3px</span>;<span> -webkit-border-top-left-radius</span>:<span> 3px</span>;<span> border-top-left-radius</span>:<span> 3px</span>;<span> -moz-border-radius-bottomleft</span>:<span> 3px</span>;<span> -moz-border-radius-topleft</span>:<span> 3px</span>; }<span> .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>;<span> background-color</span>:<span> transparent</span>; }<span> .pagination ul .active a, .pagination ul .active span </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>; }<span> .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span </span>{<span> background-color</span>:<span> #f0c040</span>; }<span> .pagination ul li.last-child a, .pagination ul li.last-child span </span>{<span> -webkit-border-top-right-radius</span>:<span> 3px</span>;<span> border-top-right-radius</span>:<span> 3px</span>;<span> -webkit-border-bottom-right-radius</span>:<span> 3px</span>;<span> border-bottom-right-radius</span>:<span> 3px</span>;<span> -moz-border-radius-topright</span>:<span> 3px</span>;<span> -moz-border-radius-bottomright</span>:<span> 3px</span>; }<span> .pagination ul li.current a</span>{<span>color</span>:<span> #f00 </span>;<span>font-weight</span>:<span> bold</span>;<span> background</span>:<span> #ddd</span>}
TP的分页其实是很简单的:
首先你需要导入分页类。
代码如下:
$User = M('User'); // 实例化User对象
import('ORG.Util.Page');// 导入分页类
$count = $User->where('status=1')->count();// 查询满足要求的总记录数
$Page = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
$show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display(); // 输出模板
文章分页,我记得应该有个thinkphpcms的 你找找那个 安装一遍看看那个怎么写的。。你行的 - -