ThinkPHP分页类的使用相关有关问题
- WBOY원래의
- 2016-06-13 12:28:35730검색
ThinkPHP分页类的使用相关问题
ThinkPHP3.2.3<span style="color: #000000;">Page</span>.<span style="color: #0000ff;">class</span>.<span style="color: #000000;">php文件源码</span><span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: 麦当苗儿 <http:>// +----------------------------------------------------------------------</http:></span><span style="color: #000000;">namespace Think;</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Page{ </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$firstRow</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 起始行数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$listRows</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 列表每页显示行数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$parameter</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 分页跳转时要带的参数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$totalRows</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 总行数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$totalPages</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 分页总页面数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$rollPage</span> = 11;<span style="color: #008000;">//</span><span style="color: #008000;"> 分页栏每页显示的页数</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$lastSuffix</span> = <span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 最后一页是否显示总页数</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$p</span> = 'p'; <span style="color: #008000;">//</span><span style="color: #008000;">分页参数名</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$url</span> = ''; <span style="color: #008000;">//</span><span style="color: #008000;">当前链接URL</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$nowPage</span> = 1<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分页显示定制</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span>'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>', 'prev' => ' '>>', 'first' => '1...', 'last' => '...%TOTAL_PAGE%', 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',<span style="color: #000000;"> ); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 架构函数 * @param array $totalRows 总的记录数 * @param array $listRows 每页显示记录数 * @param array $parameter 分页跳转的参数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$totalRows</span>, <span style="color: #800080;">$listRows</span>=20, <span style="color: #800080;">$parameter</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">()) { C(</span>'VAR_PAGE') && <span style="color: #800080;">$this</span>->p = C('VAR_PAGE'); <span style="color: #008000;">//</span><span style="color: #008000;">设置分页参数名称</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> 基础设置 </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$this</span>->totalRows = <span style="color: #800080;">$totalRows</span>; <span style="color: #008000;">//</span><span style="color: #008000;">设置总记录数</span> <span style="color: #800080;">$this</span>->listRows = <span style="color: #800080;">$listRows</span>; <span style="color: #008000;">//</span><span style="color: #008000;">设置每页显示行数</span> <span style="color: #800080;">$this</span>->parameter = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$parameter</span>) ? <span style="color: #800080;">$_GET</span> : <span style="color: #800080;">$parameter</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->nowPage = <span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>[<span style="color: #800080;">$this</span>->p]) ? 1 : <span style="color: #008080;">intval</span>(<span style="color: #800080;">$_GET</span>[<span style="color: #800080;">$this</span>-><span style="color: #000000;">p]); </span><span style="color: #800080;">$this</span>->nowPage = <span style="color: #800080;">$this</span>->nowPage>0 ? <span style="color: #800080;">$this</span>->nowPage : 1<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->firstRow = <span style="color: #800080;">$this</span>->listRows * (<span style="color: #800080;">$this</span>->nowPage - 1<span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 定制分页链接设置 * @param string $name 设置名称 * @param string $value 设置值 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setConfig(<span style="color: #800080;">$name</span>,<span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->config[<span style="color: #800080;">$name</span><span style="color: #000000;">])) { </span><span style="color: #800080;">$this</span>->config[<span style="color: #800080;">$name</span>] = <span style="color: #800080;">$value</span><span style="color: #000000;">; } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 生成链接URL * @param integer $page 页码 * @return string </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> url(<span style="color: #800080;">$page</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">str_replace</span>(<span style="color: #008080;">urlencode</span>('[PAGE]'), <span style="color: #800080;">$page</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">url); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 组装分页链接 * @return string </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show() { </span><span style="color: #0000ff;">if</span>(0 == <span style="color: #800080;">$this</span>->totalRows) <span style="color: #0000ff;">return</span> ''<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 生成URL </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$this</span>->parameter[<span style="color: #800080;">$this</span>->p] = '[PAGE]'<span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->url = U(ACTION_NAME, <span style="color: #800080;">$this</span>-><span style="color: #000000;">parameter); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 计算分页信息 </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$this</span>->totalPages = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$this</span>->totalRows / <span style="color: #800080;">$this</span>->listRows); <span style="color: #008000;">//</span><span style="color: #008000;">总页数</span> <span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->totalPages) && <span style="color: #800080;">$this</span>->nowPage > <span style="color: #800080;">$this</span>-><span style="color: #000000;">totalPages) { </span><span style="color: #800080;">$this</span>->nowPage = <span style="color: #800080;">$this</span>-><span style="color: #000000;">totalPages; } </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 计算分页临时变量 </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$now_cool_page</span> = <span style="color: #800080;">$this</span>->rollPage/2<span style="color: #000000;">; </span><span style="color: #800080;">$now_cool_page_ceil</span> = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$now_cool_page</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->lastSuffix && <span style="color: #800080;">$this</span>->config['last'] = <span style="color: #800080;">$this</span>-><span style="color: #000000;">totalPages; </span><span style="color: #008000;">//</span><span style="color: #008000;">上一页</span> <span style="color: #800080;">$up_row</span> = <span style="color: #800080;">$this</span>->nowPage - 1<span style="color: #000000;">; </span><span style="color: #800080;">$up_page</span> = <span style="color: #800080;">$up_row</span> > 0 ? '<a class="prev" href="'%20.%20<span%20style=" color:>$this->url(<span style="color: #800080;">$up_row</span>) . '">' . <span style="color: #800080;">$this</span>->config['prev'] . '</a>' : ''<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">下一页</span> <span style="color: #800080;">$down_row</span> = <span style="color: #800080;">$this</span>->nowPage + 1<span style="color: #000000;">; </span><span style="color: #800080;">$down_page</span> = (<span style="color: #800080;">$down_row</span> $this->totalPages) ? '<a class="next" href="'%20.%20<span%20style=" color:>$this->url(<span style="color: #800080;">$down_row</span>) . '">' . <span style="color: #800080;">$this</span>->config['next'] . '</a>' : ''<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">第一页</span> <span style="color: #800080;">$the_first</span> = ''<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->totalPages > <span style="color: #800080;">$this</span>->rollPage && (<span style="color: #800080;">$this</span>->nowPage - <span style="color: #800080;">$now_cool_page</span>) >= 1<span style="color: #000000;">){ </span><span style="color: #800080;">$the_first</span> = '<a class="first" href="'%20.%20<span%20style=" color:>$this->url(1) . '">' . <span style="color: #800080;">$this</span>->config['first'] . '</a>'<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">最后一页</span> <span style="color: #800080;">$the_end</span> = ''<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->totalPages > <span style="color: #800080;">$this</span>->rollPage && (<span style="color: #800080;">$this</span>->nowPage + <span style="color: #800080;">$now_cool_page</span>) $this-><span style="color: #000000;">totalPages){ </span><span style="color: #800080;">$the_end</span> = '<a class="end" href="'%20.%20<span%20style=" color:>$this->url(<span style="color: #800080;">$this</span>->totalPages) . '">' . <span style="color: #800080;">$this</span>->config['last'] . '</a>'<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">数字连接</span> <span style="color: #800080;">$link_page</span> = ""<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 1; <span style="color: #800080;">$i</span> $this->rollPage; <span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>((<span style="color: #800080;">$this</span>->nowPage - <span style="color: #800080;">$now_cool_page</span>) ){ <span style="color: #800080;">$page</span> = <span style="color: #800080;">$i</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">elseif</span>((<span style="color: #800080;">$this</span>->nowPage + <span style="color: #800080;">$now_cool_page</span> - 1) >= <span style="color: #800080;">$this</span>-><span style="color: #000000;">totalPages){ </span><span style="color: #800080;">$page</span> = <span style="color: #800080;">$this</span>->totalPages - <span style="color: #800080;">$this</span>->rollPage + <span style="color: #800080;">$i</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$page</span> = <span style="color: #800080;">$this</span>->nowPage - <span style="color: #800080;">$now_cool_page_ceil</span> + <span style="color: #800080;">$i</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> > 0 && <span style="color: #800080;">$page</span> != <span style="color: #800080;">$this</span>-><span style="color: #000000;">nowPage){ </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> $this-><span style="color: #000000;">totalPages){ </span><span style="color: #800080;">$link_page</span> .= '<a class="num" href="'%20.%20<span%20style=" color:>$this->url(<span style="color: #800080;">$page</span>) . '">' . <span style="color: #800080;">$page</span> . '</a>'<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span> > 0 && <span style="color: #800080;">$this</span>->totalPages != 1<span style="color: #000000;">){ </span><span style="color: #800080;">$link_page</span> .= '<span class="current">' . <span style="color: #800080;">$page</span> . '</span>'<span style="color: #000000;">; } } } </span><span style="color: #008000;">//</span><span style="color: #008000;">替换分页内容</span> <span style="color: #800080;">$page_str</span> = <span style="color: #008080;">str_replace</span><span style="color: #000000;">( </span><span style="color: #0000ff;">array</span>('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>->config['header'], <span style="color: #800080;">$this</span>->nowPage, <span style="color: #800080;">$up_page</span>, <span style="color: #800080;">$down_page</span>, <span style="color: #800080;">$the_first</span>, <span style="color: #800080;">$link_page</span>, <span style="color: #800080;">$the_end</span>, <span style="color: #800080;">$this</span>->totalRows, <span style="color: #800080;">$this</span>->totalPages), <span style="color: #800080;">$this</span>->config['theme'<span style="color: #000000;">]); </span><span style="color: #0000ff;">return</span> "<div>{<span style="color: #800080;">$page_str</span>}</div>"<span style="color: #000000;">; }}</span>
通常在数据查询后都会对数据集进行分页操作,ThinkPHP也提供了分页类来对数据分页提供支持。 下面是数据分页的两种示例。
第一种:利用Page类和limit方法
<span style="color: #800080;">$User</span> = M('User'); <span style="color: #008000;">//</span><span style="color: #008000;"> 实例化User对象</span><span style="color: #800080;">$count</span> = <span style="color: #800080;">$User</span>->where('status=1')-><span style="color: #008080;">count</span>();<span style="color: #008000;">//</span><span style="color: #008000;"> 查询满足要求的总记录数</span><span style="color: #800080;">$Page</span> = <span style="color: #0000ff;">new</span> \Think\Page(<span style="color: #800080;">$count</span>,25);<span style="color: #008000;">//</span><span style="color: #008000;"> 实例化分页类 传入总记录数和每页显示的记录数(25)</span><span style="color: #800080;">$show</span> = <span style="color: #800080;">$Page</span>->show();<span style="color: #008000;">//</span><span style="color: #008000;"> 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性</span><span style="color: #800080;">$list</span> = <span style="color: #800080;">$User</span>->where('status=1')->order('create_time')->limit(<span style="color: #800080;">$Page</span>->firstRow.','.<span style="color: #800080;">$Page</span>->listRows)-><span style="color: #000000;">select();</span><span style="color: #800080;">$this</span>->assign('list',<span style="color: #800080;">$list</span>);<span style="color: #008000;">//</span><span style="color: #008000;"> 赋值数据集</span><span style="color: #800080;">$this</span>->assign('page',<span style="color: #800080;">$show</span>);<span style="color: #008000;">//</span><span style="color: #008000;"> 赋值分页输出</span><span style="color: #800080;">$this</span>->display(); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出模板</span>
第二种:分页类和page方法的实现
<span style="color: #800080;">$User</span> = M('User'); <span style="color: #008000;">//</span><span style="color: #008000;"> 实例化User对象// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取</span><span style="color: #800080;">$list</span> = <span style="color: #800080;">$User</span>->where('status=1')->order('create_time')->page(<span style="color: #800080;">$_GET</span>['p'].',25')-><span style="color: #000000;">select();</span><span style="color: #800080;">$this</span>->assign('list',<span style="color: #800080;">$list</span>);<span style="color: #008000;">//</span><span style="color: #008000;"> 赋值数据集</span><span style="color: #800080;">$count</span> = <span style="color: #800080;">$User</span>->where('status=1')-><span style="color: #008080;">count</span>();<span style="color: #008000;">//</span><span style="color: #008000;"> 查询满足要求的总记录数</span><span style="color: #800080;">$Page</span> = <span style="color: #0000ff;">new</span> \Think\Page(<span style="color: #800080;">$count</span>,25);<span style="color: #008000;">//</span><span style="color: #008000;"> 实例化分页类 传入总记录数和每页显示的记录数</span><span style="color: #800080;">$show</span> = <span style="color: #800080;">$Page</span>->show();<span style="color: #008000;">//</span><span style="color: #008000;"> 分页显示输出</span><span style="color: #800080;">$this</span>->assign('page',<span style="color: #800080;">$show</span>);<span style="color: #008000;">//</span><span style="color: #008000;"> 赋值分页输出</span><span style="color: #800080;">$this</span>->display(); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出模板</span>
带入查询条件
如果是POST方式查询,如何确保分页之后能够保持原先的查询条件呢,我们可以给分页类传入参数,方法是给分页类的parameter属性赋值
<span style="color: #800080;">$count</span> = <span style="color: #800080;">$User</span>->where(<span style="color: #800080;">$map</span>)-><span style="color: #008080;">count</span>();<span style="color: #008000;">//</span><span style="color: #008000;"> 查询满足要求的总记录数</span><span style="color: #800080;">$Page</span> = <span style="color: #0000ff;">new</span> \Think\Page(<span style="color: #800080;">$count</span>,25);<span style="color: #008000;">//</span><span style="color: #008000;"> 实例化分页类 传入总记录数和每页显示的记录数//分页跳转的时候保证查询条件</span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$map</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span>=><span style="color: #800080;">$val</span><span style="color: #000000;">) { </span><span style="color: #800080;">$Page</span>->parameter[<span style="color: #800080;">$key</span>] = <span style="color: #008080;">urlencode</span>(<span style="color: #800080;">$val</span><span style="color: #000000;">);}</span><span style="color: #800080;">$show</span> = <span style="color: #800080;">$Page</span>->show();<span style="color: #008000;">//</span><span style="color: #008000;"> 分页显示输出</span>
分页样式定制
我们可以对输出的分页样式进行定制,分页类Page提供了一个setConfig方法来修改默认的一些设置。例如:
<span style="color: #800080;">$page</span>->setConfig('header','
共%TOTAL_ROW%条记录 第%NOW_PAGE%页/共%TOTAL_PAGE%页 '); $page->setConfig('prev', '上一页'); $page->setConfig('next', '下一页'); $page->setConfig('first', '首页'); $page->setConfig('last', '尾页'); $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');setConfig方法支持的属性包括:
header:头部描述信息,默认值 “共 %TOTAL_ROW% 条记录”
next:下一页描述信息,默认值 “>>”
first:第一页描述信息,默认值 “1…”
last:最后一页描述信息,默认值 “…%TOTAL_PAGE%”
theme :分页主题描述信息,包括了上面所有元素的组合 ,设置该属性可以改变分页的各个单元的显示位置,默认值是 “%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%”
其中,显示位置的对应的关系为:
位置 说明
%FIRST% 表示第一页的链接显示
%UP_PAGE% 表示上一页的链接显示
%LINK_PAGE% 表示分页的链接显示
%DOWN_PAGE% 表示下一页的链接显示
%END% 表示最后一页的链接显示
除了改变显示信息外,你还可以使用样式来定义分页的显示效果。 这些样式class包括:first(第一页)、prev(上一页)、next(下一页)、end(最后一页)、num(其他页的数字)、current(当前页)。
ThinkPHP系统分页类写的已经很完善只要花几分钟时间研究一下源代码即可,官方注释也很清晰。这里注解一下使用中的问题。系统默认设置了
<code class="hljs cs has-numbering"><span class="hljs-keyword">public $rollPage = <span class="hljs-number">11;<span class="hljs-comment">// 分页栏每页显示的页数<span class="hljs-keyword">public $lastSuffix = <span class="hljs-keyword">true; <span class="hljs-comment">// 最后一页是否显示总页数</span></span></span></span></span></span></code><br><br>
一开始看注释还不是很清楚,其实第一个参数是控制显示多少页数,如果默认显示11个页码一般太长,个人喜欢设置为5,当然修改这些参数不要在系统源文件中修改,而是在使用的时候设置$page->rollPage=5
即可。
而第二个参数就比较坑人了,我们在个性化分页显示的时候通常会采用setConfig('last','尾页');
用来设置最后一页显示为“尾页”,但是在show方法中可以看到tp在$this->lastSuffix
为真的时候重置该$this->config['last']
的值为总页数这样导致无论你怎么设置最后一业都显示为数字。也不知道官方为什么这么设置,解决办法依然是重新设置$page->lastSuffix = false;
即可。
성명:본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.이전 기사:PHP和JAVA双语言复建项目다음 기사:小弟我做PHP,但是小弟我要批判下整天唱衰.NET的淫