Heim > Backend-Entwicklung > PHP-Tutorial > ThinkPHP分页类的使用相关问题,thinkphp分页_PHP教程

ThinkPHP分页类的使用相关问题,thinkphp分页_PHP教程

WBOY
Freigeben: 2016-07-12 08:59:08
Original
892 Leute haben es durchsucht

ThinkPHP分页类的使用相关问题,thinkphp分页

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>
Nach dem Login kopieren

通常在数据查询后都会对数据集进行分页操作,ThinkPHP也提供了分页类来对数据分页提供支持。 下面是数据分页的两种示例。

第一种:利用Page类和limit方法

<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> 实例化分页类 传入总记录数和每页显示的记录数(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>
Nach dem Login kopieren

第二种:分页类和page方法的实现

<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> 实例化分页类 传入总记录数和每页显示的记录数</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>
Nach dem Login kopieren

带入查询条件
如果是POST方式查询,如何确保分页之后能够保持原先的查询条件呢,我们可以给分页类传入参数,方法是给分页类的parameter属性赋值

<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> 实例化分页类 传入总记录数和每页显示的记录数
//分页跳转的时候保证查询条件</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>
Nach dem Login kopieren

分页样式定制
我们可以对输出的分页样式进行定制,分页类Page提供了一个setConfig方法来修改默认的一些设置。例如:

<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%');
Nach dem Login kopieren

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系统分页类写的已经很完善只要花几分钟时间研究一下源代码即可,官方注释也很清晰。这里注解一下使用中的问题。系统默认设置了

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 />
Nach dem Login kopieren

  一开始看注释还不是很清楚,其实第一个参数是控制显示多少页数,如果默认显示11个页码一般太长,个人喜欢设置为5,当然修改这些参数不要在系统源文件中修改,而是在使用的时候设置$page->rollPage=5即可。

  而第二个参数就比较坑人了,我们在个性化分页显示的时候通常会采用setConfig('last','尾页'); 用来设置最后一页显示为“尾页”,但是在show方法中可以看到tp在$this->lastSuffix 为真的时候重置该$this->config['last']的值为总页数这样导致无论你怎么设置最后一业都显示为数字。也不知道官方为什么这么设置,解决办法依然是重新设置$page->lastSuffix = false; 即可。
分页类

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1100149.htmlTechArticleThinkPHP分页类的使用相关问题,thinkphp分页 ThinkPHP3.2.3 Page . class . php文件源码 ? php // +---------------------------------------------------------------------...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage