PHP教程

php自定义分页类

本文主要是介绍php自定义分页类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?php
class Paging
{
    private $totalStrip;  //总条数
    private $pageStrip;   //每页条数
    private $totalPages;  //总页数
    private $url;         //当前页面的url
    private $nowPage;     //当前的页码
    private $aroundPage;  //当前页左右条数
    public function __construct($totalStrip,$pageStrip,$aroundPage)
    {
        $this->totalStrip = $totalStrip;           //总条数
        $this->pageStrip = $pageStrip??5;          //每页条数
        $this->totalPages = $this->getTotalPages();//总页数
        $this->url = $this->getUrl();              //当前页面的url
        $this->nowPage = $this->getNowPage();      //当前的页码
        $this->aroundPage = $aroundPage??2;        //当前页左右条数
    }
    //总页数
    private function getTotalPages()
    {
        return ceil($this->totalStrip/$this->pageStrip);
    }
    //当前页面的url
    private function getUrl()
    {
        return $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    }
    //当前的页码
    private function getNowPage()
    {
        return isset($_GET['page'])?$_GET['page']:1;
    }
    //首页
    private function firstPage()
    {
        return "<a href='{$this->url}?page=1'>首页</a>";
    }
    //上一页
    private function prevPage()
    {
        $prevPage = $this->nowPage - 1;
        return "<a href='{$this->url}?page={$prevPage}'>上一页</a>";
    }
    //当前页之前
    private function leftPage()
    {
        $str = '';
        for($i=$this->aroundPage; $i >= 1; $i--){
            $Page = $this->nowPage-$i;
            if($Page > 0){
                $str .= "<a href='{$this->url}?page=$Page'>$Page</a>";
            }

        }
        return $str;
    }
    //当前页
    private function toNowPage()
    {
        return "<a>$this->nowPage</a>";
    }
    //当前页之后
    private function rightPage()
    {
        $str = '';
        for($i=1; $i <= $this->aroundPage; $i++){
            $Page = $this->nowPage+$i;
            if($Page < $this->totalPages) {
                $str .= "<a href='{$this->url}?page=$Page'>$Page</a>";
            }
        }
        return $str;
    }
    //下一页
    private function nextPage()
    {
        $nextPage = $this->nowPage + 1;
        return "<a href='{$this->url}?page={$nextPage}'>下一页</a>";
    }
    //尾页
    private function lastPage()
    {
        return "<a href='{$this->url}?page=$this->totalPages'>尾页</a>";
    }
    public function show()
    {
        $str = "";
        if($this->nowPage > 1){
            $str .= $this->firstPage();
            $str .= $this->prevPage();
            $str .= $this->leftPage();
        }


        $str .= $this->toNowPage();
        if($this->nowPage < $this->totalPages){
            $str .= $this->rightPage();
            $str .= $this->nextPage();
            $str .= $this->lastPage();
        }

        return $str;
    }
}
//$a = new Paging(20,3,2);
//echo $a->show();

  

这篇关于php自定义分页类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!