Home > php教程 > php手册 > body text

结合 thinkPHP 分页写成自己分页类

WBOY
Release: 2016-06-13 11:29:44
Original
1228 people have browsed it

Copy after login
Copy after login
Copy after login
<?<span php 
</span><span //</span><span  +----------------------------------------------------------------------
// | 参考  ThinkPHP 分页类改变而来
// +----------------------------------------------------------------------
// | 有两种样式:1、当面页码始终在中间。 2、当点到分页条两边的页码时,自动翻页(默认)。
// +----------------------------------------------------------------------
// | Author: zbseoag <zbsoeagy@qq.com>
// +----------------------------------------------------------------------
// $Id: page.class.php  2013-05-26 00:08:49</span>

 <span session_start</span>(); <span //</span><span 由于要记录用户是向前,还是向后翻页,故启用了session</span>

<span class</span><span  Page{

    </span><span public</span> <span $page</span>; <span //</span><span 当前页</span>
    <span public</span> <span $nextPage</span>; <span //</span><span 上一页</span>
    <span public</span> <span $prevPage</span>; <span //</span><span 下一页</span>
    <span public</span> <span $pageMax</span>; <span //</span><span 最大页</span>
    <span public</span> <span $startPage</span>; <span //</span><span 分页条起码</span>
    <span public</span> <span $endPage</span>;  <span //</span><span  分页条止码</span>
    <span public</span> <span $style</span>; <span //</span><span 样式:$style = mid</span>
    <span public</span> <span $parameter</span>;<span //</span><span 页数跳转时要带的参数</span>
    <span public</span> <span $varPage</span>;<span //</span><span 默认分页变量</span>
    <span public</span> <span $url</span> = <span null</span>;  <span //</span><span 指定url地址</span>
    
    <span protected</span> <span $config</span> = <span array</span><span (
        </span>'header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'尾页',
        'theme' => '%records% %header% %page%/%pageMax% 页  %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%'<span 
    );
     
    </span><span /*</span><span *
     +----------------------------------------------------------
     * 架造函数
     +----------------------------------------------------------
     * @param  $records   总的记录数
     * @param  $pageSize  每页显示记录数
     * @param  $pageVal   分页跳转的参数
     * @param  $parameter 附加参数
     +----------------------------------------------------------
    </span><span */</span>
     
    <span function</span> __construct(<span $records</span>, <span $pageSize</span>, <span $pageVal</span> = 'p', <span $parameter</span> = <span null</span><span ){
         
        </span><span $this</span>->records = <span $records</span><span ;
        </span><span $this</span>->pageMax = <span ceil</span>(<span $records</span>/<span $pageSize</span><span );
        </span><span $this</span>->page= <span max</span>(<span intval</span>(<span $_GET</span>[<span $pageVal</span>]), 1); <span //</span><span 最小为1</span>
        <span $this</span>->page = <span min</span>(<span $this</span>->page, <span $this</span>->pageMax); <span //</span><span 最大为末页</span>
        <span $this</span>->nextPage = <span $this</span>->page + 1<span ;
        </span><span $this</span>->prevPage = <span $this</span>->page - 1<span ;
        </span><span $this</span>->varPage = <span $pageVal</span>; <span //</span><span 默认分页变量名</span>
        <span $this</span>->parameter = <span $parameter</span>; <span //</span><span 附加 url 参数</span>
<span     }
     
    </span><span /*</span><span *
     +----------------------------------------------------------
     * 设置样式
     +----------------------------------------------------------
     * @param  $name   内置参数名
     * @param  $value  给定参数值
     +----------------------------------------------------------
     </span><span */</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 *
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @param  $length 分页条长度
     +----------------------------------------------------------
     * @param  $hide 是否适时隐藏上、下一页,比如:当前是第一页,则隐藏上一页
     +----------------------------------------------------------
     </span><span */</span>
    <span function</span> show(<span $length</span>, <span $hide</span> = <span false</span><span ){

        </span><span if</span>(0 == <span $this</span>->records) <span return</span> ''<span ;
        </span><span empty</span>(<span $this</span>->url)? <span $this</span>->url = <span $_SERVER</span>['REQUEST_URI'] : <span null</span><span ;
        </span><span $url</span> = <span $this</span>->url.(<span strpos</span>(<span $this</span>->url,'?')?'':'?').<span $this</span>-><span parameter;
        </span><span $parse</span> = <span parse_url</span>(<span $url</span><span );
         
        </span><span if</span>(<span isset</span>(<span $parse</span>['query'<span ])){
            </span><span parse_str</span>(<span $parse</span>['query'],<span $params</span><span );
            </span><span unset</span>(<span $params</span>[<span $this</span>-><span varPage]);
            </span><span $url</span> = <span $parse</span>['path'].'?'.<span http_build_query</span>(<span $params</span><span );
        }

        </span><span if</span>('mid' == <span $this</span>->style){ <span //</span><span 当前页居中</span>
            <span if</span>(<span $length</span> % 2 != 0<span  ){
                </span><span $midder</span> = <span ceil</span>(<span $length</span> / 2<span );
                </span><span $big_repair</span> = <span $midder</span> - 1 ;<span //</span><span 当上面以进一法取整,则这里为减1,反之为加1</span>
            }<span else</span><span {
                </span><span $big_repair</span> = <span $midder</span> = <span $length</span> / 2<span ;
            }
            </span><span $sml_repair</span> = <span $midder</span>- 1;  <span //</span><span 这是小补丁</span>
            <span $this</span>->startPage = (<span $this</span>->page  + <span $midder</span>) > <span $this</span>->pageMax ? <span $this</span>->pageMax  - <span $length</span> : <span $this</span>->page - <span $sml_repair</span><span ;
            </span><span $this</span>->endPage = (<span $this</span>->page < <span $midder</span>)? <span $length</span> : <span $this</span>->page + <span $big_repair</span><span ;
        }</span><span else</span>{ <span //</span><span 最后一页翻页</span>
            <span $this</span>->startPage = (<span $this</span>->page >= <span $length</span>)? <span $_SESSION</span>['startPage'] : 1<span ;
            </span><span $this</span>->endPage  = <span $this</span>->startPage + <span $length</span> - 1<span ;
             
            </span><span $this</span>->lastPage =  <span isset</span>(<span $_SESSION</span>['lastPage'])? <span $_SESSION</span>['lastPage'] : 1; <span //</span><span 上次的页码</span>
            <span $_SESSION</span>['lastPage'] = <span $this</span>->page;  <span //</span><span 保存当前页</span>
             
            <span if</span>(<span $this</span>->lastPage < <span $this</span>->page){ <span //</span><span 后翻页</span>
                <span if</span>(<span $this</span>->page >= <span $this</span>-><span endPage){
                    </span><span $this</span>->startPage = <span $_SESSION</span>['startPage'] = <span $this</span>-><span page;
                    </span><span $this</span>->endPage = <span $this</span>->startPage + <span $length</span> -1<span ;
                }
            }</span><span elseif</span>(<span $this</span>->lastPage == <span $this</span>->page){<span //</span><span 切换</span>
                <span if</span>(<span $this</span>->page == <span $this</span>-><span endPage){
                    </span><span $this</span>->startPage = <span $_SESSION</span>['startPage'] = <span $this</span>-><span page;
                    </span><span $this</span>->endPage = <span $this</span>->startPage + <span $length</span> -1<span ;
                }</span><span else</span><span {
                    </span><span $this</span>->startPage = <span $_SESSION</span>['startPage'] = <span $this</span>->page - <span $length</span> + 1<span ;
                    </span><span $this</span>->endPage = <span $this</span>->startPage + <span $length</span> -1<span ;
                }
            }</span><span else</span>{<span //</span><span 前翻页</span>
                <span if</span>(<span $this</span>->page == <span $this</span>-><span startPage ){
                    </span><span $this</span>->startPage = <span $_SESSION</span>['startPage'] = <span $this</span>->page - <span $length</span> + 1<span ;
                    </span><span $this</span>->endPage = <span $this</span>->startPage + <span $length</span> -1<span ;
                }
            }
            </span><span //</span><span echo '开始页码:'. $this->startPage.'<br/>';
            //echo '上次页码:'. $this->lastPage.'<br/>';</span>
<span         }
        </span><span //</span><span 修正一下</span>
        <span $this</span>->startPage = <span min</span>(<span $this</span>->pageMax - <span $length</span> + 1, <span $this</span>->startPage); <span //</span><span 最大</span>
        <span $this</span>->startPage = <span max</span>(<span $this</span>->startPage, 1);<span //</span><span 最小为第一页(先求最大,再求最小)</span>
         
        <span $this</span>->endPage = <span $this</span>->startPage + <span $length</span> -1<span ;
        </span><span $this</span>->endPage = <span min</span>(<span $this</span>->pageMax, <span $this</span>->endPage); <span //</span><span 最大为末页</span>
         
        <span $_SESSION</span>['startPage'] = <span $this</span>->startPage; <span //</span><span 修正之后,重新保存
         
        //echo '开始页码:'. $this->startPage.'<br/>';
        //echo '上次页码:'. $this->lastPage.'<br/>';</span>
         
        <span if</span>(<span $this</span>->page <= 1){<span //</span><span 如果已经是首页</span>
            <span $firstPage</span> = '<span>'.<span $this</span>->config['first'].'</span>'<span ;
            </span><span $prevPage</span> = '<span>'.<span $this</span>->config['prev'].'</span>'<span ;
        }</span><span else</span><span {
            </span><span $firstPage</span> = "<span><a href='<span $url</span>&<span $this</span>->varPage=1'>".<span $this</span>->config['first'].'</a>'<span ;
            </span><span $prevPage</span> = "<span><a href='<span $url</span>&<span $this</span>->varPage=<span $this</span>->prevPage'>".<span $this</span>->config['prev'].'</a>'<span ;
        }
        
        </span><span if</span>(<span $this</span>->page >= <span $this</span>->pageMax){ <span //</span><span 如果已经是末页</span>
            <span $nextPage</span> = '<span>'.<span $this</span>->config['next'].'</span>'<span ;
            </span><span $lastPage</span> = '<span>'.<span $this</span>->config['last'].'</span>'<span ;
        }</span><span else</span><span {
            </span><span $lastPage</span> = "<a href='<span $url</span>&<span $this</span>->varPage=<span $this</span>->pageMax'>".<span $this</span>->config['last'].'</a>'<span ;
            </span><span $nextPage</span> = "<a href='<span $url</span>&<span $this</span>->varPage=<span $this</span>->nextPage'>".<span $this</span>->config['next'].'</a>'<span ;
        }
        
        </span><span if</span>(<span $hide</span>){ <span //</span><span 如果是隐藏模式</span>
            (<span $this</span>->nextPage >= <span $this</span>->pageMax)? <span $nextPage</span> = '' : <span null</span><span ;
            (</span><span $this</span>->prevPage < 1)? <span $prevPage</span> = '' : <span null</span><span ;
        }

        </span><span for</span>(<span $i</span> = <span $this</span>->startPage; <span $i</span> <= <span $this</span>->endPage; <span $i</span>++<span ){
            </span><span if</span> (<span $this</span>->page == <span $i</span><span )
                </span><span $linkPage</span> .= "<a class='current' href='<span $url</span>&<span $this</span>->varPage=<span $i</span>'><span $i</span></a>"<span ;
            </span><span else</span>
                <span $linkPage</span> .= "<a href='<span $url</span>&<span $this</span>->varPage=<span $i</span>'><span $i</span></a>"<span ;
        }
        </span><span $search</span> = <span array</span>('%records%','%header%','%page%','%pageMax%','%firstPage%','%prevPage%','%linkPage%','%nextPage%','%lastPage%'<span );
        </span><span $replace</span> = <span array</span>(<span $this</span>->records,<span $this</span>->config['header'],<span $this</span>->page,<span $this</span>->pageMax,<span $firstPage</span>,<span $prevPage</span>,<span $linkPage</span>,<span $nextPage</span>,<span $lastPage</span><span );
        </span><span $pageStr</span> = <span str_replace</span>(<span $search</span>, <span $replace</span>, <span $this</span>->config['theme'<span ]);

        </span><span return</span> <span $pageStr</span><span ;
    }
     
}
</span><span $page</span> = <span new</span> Page(340, 10<span );
</span><span $theme</span> = '%records% %header% %page%/%pageMax% 页  %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%'<span ;
</span><span $page</span>->setConfig('theme',<span $theme</span>); <span //</span><span 可以修改样式,比如:当不要末页是,可以删除 %lastPage%,或者改变位置次序</span>
<span $page</span>->setConfig('last','末页'); <span //</span><span 可自定义文字 : 'header'='条记录','prev'='上一页','next'='下一页','first'='首页','last'='尾页'</span>
<span $page</span>->url = 'http://localhost/test.php'<span ;
</span><span echo</span> <span $page</span>->show(10<span );

</span>?>
 <style><span 
a</span>,span{ margin:<span 2px 5px;}
</span>.<span current</span>{ color:<span red;}
</span></style>
Copy after login
Copy after login
Copy after login
Copy after login

Copy after login
Copy after login
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template