Home > Article > Backend Development > Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial
ThinkPHP3.2.3<span> Page</span>.<span>class</span>.<span>php文件源码 </span><?<span>php </span><span>//</span><span> +---------------------------------------------------------------------- // | 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: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn> // +----------------------------------------------------------------------</span> <span>namespace Think; </span><span>class</span><span> Page{ </span><span>public</span> <span>$firstRow</span>; <span>//</span><span> 起始行数</span> <span>public</span> <span>$listRows</span>; <span>//</span><span> 列表每页显示行数</span> <span>public</span> <span>$parameter</span>; <span>//</span><span> 分页跳转时要带的参数</span> <span>public</span> <span>$totalRows</span>; <span>//</span><span> 总行数</span> <span>public</span> <span>$totalPages</span>; <span>//</span><span> 分页总页面数</span> <span>public</span> <span>$rollPage</span> = 11;<span>//</span><span> 分页栏每页显示的页数</span> <span>public</span> <span>$lastSuffix</span> = <span>true</span>; <span>//</span><span> 最后一页是否显示总页数</span> <span>private</span> <span>$p</span> = 'p'; <span>//</span><span>分页参数名</span> <span>private</span> <span>$url</span> = ''; <span>//</span><span>当前链接URL</span> <span>private</span> <span>$nowPage</span> = 1<span>; </span><span>//</span><span> 分页显示定制</span> <span>private</span> <span>$config</span> = <span>array</span><span>( </span>'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>', 'prev' => '<<', 'next' => '>>', 'first' => '1...', 'last' => '...%TOTAL_PAGE%', 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',<span> ); </span><span>/*</span><span>* * 架构函数 * @param array $totalRows 总的记录数 * @param array $listRows 每页显示记录数 * @param array $parameter 分页跳转的参数 </span><span>*/</span> <span>public</span> <span>function</span> __construct(<span>$totalRows</span>, <span>$listRows</span>=20, <span>$parameter</span> = <span>array</span><span>()) { C(</span>'VAR_PAGE') && <span>$this</span>->p = C('VAR_PAGE'); <span>//</span><span>设置分页参数名称</span> <span>/*</span><span> 基础设置 </span><span>*/</span> <span>$this</span>->totalRows = <span>$totalRows</span>; <span>//</span><span>设置总记录数</span> <span>$this</span>->listRows = <span>$listRows</span>; <span>//</span><span>设置每页显示行数</span> <span>$this</span>->parameter = <span>empty</span>(<span>$parameter</span>) ? <span>$_GET</span> : <span>$parameter</span><span>; </span><span>$this</span>->nowPage = <span>empty</span>(<span>$_GET</span>[<span>$this</span>->p]) ? 1 : <span>intval</span>(<span>$_GET</span>[<span>$this</span>-><span>p]); </span><span>$this</span>->nowPage = <span>$this</span>->nowPage>0 ? <span>$this</span>->nowPage : 1<span>; </span><span>$this</span>->firstRow = <span>$this</span>->listRows * (<span>$this</span>->nowPage - 1<span>); } </span><span>/*</span><span>* * 定制分页链接设置 * @param string $name 设置名称 * @param string $value 设置值 </span><span>*/</span> <span>public</span> <span>function</span> setConfig(<span>$name</span>,<span>$value</span><span>) { </span><span>if</span>(<span>isset</span>(<span>$this</span>->config[<span>$name</span><span>])) { </span><span>$this</span>->config[<span>$name</span>] = <span>$value</span><span>; } } </span><span>/*</span><span>* * 生成链接URL * @param integer $page 页码 * @return string </span><span>*/</span> <span>private</span> <span>function</span> url(<span>$page</span><span>){ </span><span>return</span> <span>str_replace</span>(<span>urlencode</span>('[PAGE]'), <span>$page</span>, <span>$this</span>-><span>url); } </span><span>/*</span><span>* * 组装分页链接 * @return string </span><span>*/</span> <span>public</span> <span>function</span><span> show() { </span><span>if</span>(0 == <span>$this</span>->totalRows) <span>return</span> ''<span>; </span><span>/*</span><span> 生成URL </span><span>*/</span> <span>$this</span>->parameter[<span>$this</span>->p] = '[PAGE]'<span>; </span><span>$this</span>->url = U(ACTION_NAME, <span>$this</span>-><span>parameter); </span><span>/*</span><span> 计算分页信息 </span><span>*/</span> <span>$this</span>->totalPages = <span>ceil</span>(<span>$this</span>->totalRows / <span>$this</span>->listRows); <span>//</span><span>总页数</span> <span>if</span>(!<span>empty</span>(<span>$this</span>->totalPages) && <span>$this</span>->nowPage > <span>$this</span>-><span>totalPages) { </span><span>$this</span>->nowPage = <span>$this</span>-><span>totalPages; } </span><span>/*</span><span> 计算分页临时变量 </span><span>*/</span> <span>$now_cool_page</span> = <span>$this</span>->rollPage/2<span>; </span><span>$now_cool_page_ceil</span> = <span>ceil</span>(<span>$now_cool_page</span><span>); </span><span>$this</span>->lastSuffix && <span>$this</span>->config['last'] = <span>$this</span>-><span>totalPages; </span><span>//</span><span>上一页</span> <span>$up_row</span> = <span>$this</span>->nowPage - 1<span>; </span><span>$up_page</span> = <span>$up_row</span> > 0 ? '<a class="prev" href="' . <span>$this</span>->url(<span>$up_row</span>) . '">' . <span>$this</span>->config['prev'] . '</a>' : ''<span>; </span><span>//</span><span>下一页</span> <span>$down_row</span> = <span>$this</span>->nowPage + 1<span>; </span><span>$down_page</span> = (<span>$down_row</span> <= <span>$this</span>->totalPages) ? '<a class="next" href="' . <span>$this</span>->url(<span>$down_row</span>) . '">' . <span>$this</span>->config['next'] . '</a>' : ''<span>; </span><span>//</span><span>第一页</span> <span>$the_first</span> = ''<span>; </span><span>if</span>(<span>$this</span>->totalPages > <span>$this</span>->rollPage && (<span>$this</span>->nowPage - <span>$now_cool_page</span>) >= 1<span>){ </span><span>$the_first</span> = '<a class="first" href="' . <span>$this</span>->url(1) . '">' . <span>$this</span>->config['first'] . '</a>'<span>; } </span><span>//</span><span>最后一页</span> <span>$the_end</span> = ''<span>; </span><span>if</span>(<span>$this</span>->totalPages > <span>$this</span>->rollPage && (<span>$this</span>->nowPage + <span>$now_cool_page</span>) < <span>$this</span>-><span>totalPages){ </span><span>$the_end</span> = '<a class="end" href="' . <span>$this</span>->url(<span>$this</span>->totalPages) . '">' . <span>$this</span>->config['last'] . '</a>'<span>; } </span><span>//</span><span>数字连接</span> <span>$link_page</span> = ""<span>; </span><span>for</span>(<span>$i</span> = 1; <span>$i</span> <= <span>$this</span>->rollPage; <span>$i</span>++<span>){ </span><span>if</span>((<span>$this</span>->nowPage - <span>$now_cool_page</span>) <= 0<span> ){ </span><span>$page</span> = <span>$i</span><span>; }</span><span>elseif</span>((<span>$this</span>->nowPage + <span>$now_cool_page</span> - 1) >= <span>$this</span>-><span>totalPages){ </span><span>$page</span> = <span>$this</span>->totalPages - <span>$this</span>->rollPage + <span>$i</span><span>; }</span><span>else</span><span>{ </span><span>$page</span> = <span>$this</span>->nowPage - <span>$now_cool_page_ceil</span> + <span>$i</span><span>; } </span><span>if</span>(<span>$page</span> > 0 && <span>$page</span> != <span>$this</span>-><span>nowPage){ </span><span>if</span>(<span>$page</span> <= <span>$this</span>-><span>totalPages){ </span><span>$link_page</span> .= '<a class="num" href="' . <span>$this</span>->url(<span>$page</span>) . '">' . <span>$page</span> . '</a>'<span>; }</span><span>else</span><span>{ </span><span>break</span><span>; } }</span><span>else</span><span>{ </span><span>if</span>(<span>$page</span> > 0 && <span>$this</span>->totalPages != 1<span>){ </span><span>$link_page</span> .= '<span class="current">' . <span>$page</span> . '</span>'<span>; } } } </span><span>//</span><span>替换分页内容</span> <span>$page_str</span> = <span>str_replace</span><span>( </span><span>array</span>('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), <span>array</span>(<span>$this</span>->config['header'], <span>$this</span>->nowPage, <span>$up_page</span>, <span>$down_page</span>, <span>$the_first</span>, <span>$link_page</span>, <span>$the_end</span>, <span>$this</span>->totalRows, <span>$this</span>->totalPages), <span>$this</span>->config['theme'<span>]); </span><span>return</span> "<div>{<span>$page_str</span>}</div>"<span>; } }</span>
Usually, the data set is paging after data query. ThinkPHP also provides paging class to support data paging. Below are two examples of data paging.
The first one: using Page class and limit method
<span>$User</span> = M('User'); <span>//</span><span> 实例化User对象</span> <span>$count</span> = <span>$User</span>->where('status=1')-><span>count</span>();<span>//</span><span> 查询满足要求的总记录数</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 实例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 传入总记录数和每页显示的记录数(25)</span> <span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分页显示输出 // 进行分页数据查询 注意limit方法的参数要使用Page类的属性</span> <span>$list</span> = <span>$User</span>->where('status=1')->order('create_time')->limit(<span>$Page</span>->firstRow.','.<span>$Page</span>->listRows)-><span>select(); </span><span>$this</span>->assign('list',<span>$list</span>);<span>//</span><span> 赋值数据集</span> <span>$this</span>->assign('page',<span>$show</span>);<span>//</span><span> 赋值分页输出</span> <span>$this</span>->display(); <span>//</span><span> 输出模板</span>
Second type: Implementation of paging class and page method
<span>$User</span> = M('User'); <span>//</span><span> 实例化User对象 // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取</span> <span>$list</span> = <span>$User</span>->where('status=1')->order('create_time')->page(<span>$_GET</span>['p'].',25')-><span>select(); </span><span>$this</span>->assign('list',<span>$list</span>);<span>//</span><span> 赋值数据集</span> <span>$count</span> = <span>$User</span>->where('status=1')-><span>count</span>();<span>//</span><span> 查询满足要求的总记录数</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 实例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 传入总记录数和每页显示的记录数</span> <span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分页显示输出</span> <span>$this</span>->assign('page',<span>$show</span>);<span>//</span><span> 赋值分页输出</span> <span>$this</span>->display(); <span>//</span><span> 输出模板</span>
Introduce query conditions
If it is a POST query, how to ensure that the original query conditions can be maintained after paging? We can pass in parameters to the paging class by assigning a value to the parameter attribute of the paging class
<span>$count</span> = <span>$User</span>->where(<span>$map</span>)-><span>count</span>();<span>//</span><span> 查询满足要求的总记录数</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 实例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 传入总记录数和每页显示的记录数 //分页跳转的时候保证查询条件</span> <span>foreach</span>(<span>$map</span> <span>as</span> <span>$key</span>=><span>$val</span><span>) { </span><span>$Page</span>->parameter[<span>$key</span>] = <span>urlencode</span>(<span>$val</span><span>); } </span><span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分页显示输出</span>
Pagination style customization
We can customize the output paging style. The paging class Page provides a setConfig method to modify some default settings. For example:
<span>$page</span>->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>'<span>); </span><span>$page</span>->setConfig('prev', '上一页'<span>); </span><span>$page</span>->setConfig('next', '下一页'<span>); </span><span>$page</span>->setConfig('first', '首页'<span>); </span><span>$page</span>->setConfig('last', '尾页'<span>); </span><span>$page</span>->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
The attributes supported by the setConfig method include:
header: header description information, default value "Total %TOTAL_ROW% records"
next: next page description information, default value “>>”
first: description information on the first page, default value “1…”
last: last page description information, default value “…%TOTAL_PAGE%”
theme: paging theme description information, including a combination of all the above elements. Setting this attribute can change the display position of each unit of paging. The default value is "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%"
Among them, the corresponding relationship between the display positions is:
Location Description
%FIRST% indicates the link display on the first page
%UP_PAGE% means the link to the previous page is displayed
%LINK_PAGE% indicates paginated link display
%DOWN_PAGE% indicates the link to the next page
%END% indicates the link display of the last page
In addition to changing the display information, you can also use styles to define the display effect of pagination. These style classes include: first (first page), prev (previous page), next (next page), end (last page), num (number of other pages), current (current page).
The ThinkPHP system paging class is already very complete. Just spend a few minutes studying the source code, and the official comments are also very clear. Here are some notes on problems in use. The system is set by default
<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>
It’s not very clear from the comments at first. In fact, the first parameter is to control how many pages are displayed. If 11 page numbers are displayed by default, it is usually too long. I personally like to set it to 5. Of course, do not modify these parameters in the system source file. To modify it, just set $page->rollPage=5
when using it.
The second parameter is more confusing. When we display personalized paging, we usually use setConfig('last','尾页');
to set the last page to be displayed as the "last page", but you can see tp in the show method. When $this->lastSuffix
is true, reset the value of the $this->config['last']
to the total number of pages. This will cause the last page to be displayed as a number no matter how you set it. I don’t know why the official settings are like this. The solution is still to reset $page->lastSuffix = false;
.