Heim  >  Artikel  >  php教程  >  php之分页类代码

php之分页类代码

WBOY
WBOYOriginal
2016-06-13 10:53:53833Durchsuche

[php] 

/* 

思路 

1.把地址栏的URL获取 

2.分析URL中的query部分--就是?后面传参数的部分 

3.query部分分析成数组 

4.把数组中的page单元,+1,-1,形成2个新的数组 

5.再把新数组拼接成query部分,合成上一页,下一页连接地址 

 

*/  

//分页类  

class Page {  

    public $total;  //全部条数,从数据库取出  

    public $prePage = 10; //每页的条数  

  

    protected $curr= 1; //默认当前页码  

  

    public function __construct($total,$prePage='') {  

        $this->total = $total; //把总条目信息放在total属性  

        if ($prePage > 0) {  

            $this->prePage = $prePage;   //把每页数量放在perPage属性  

        }  

  

        //计算当前页码  

        if (isset($_GET['page']) && ($_GET['page'] + 0) > 0) {  

            $this->curr = $_GET['page'] + 0;  

        }  

    }  

  

    //主体函数  

    public function showPage() {  

        if ($this->total

            return ''; //如果总条目

        }  

  

        $cnt = ceil($this->total / $this->prePage); //算总页数,进一取整  

      

        //根据当前页,算上一页,下一页  

        /* 

        分析url,有几种情况? 

        xx.php 

        xx.php?id=5 

        xx.php?page=3 

        xx.php?id=5&page=3 

        */  

          

        //最终生成的URL里边必然有page=N  

        $url = $_SERVER['REQUEST_URI'];  

        $parse = parse_url($url); //把URL分析结果放在数组里  

        //print_r($parse);  

        //保证参数里边有page  

        if (!isset($parse['query'])) {  

            $parse['query'] = 'page=' .$this->curr;  

        }  

        //把query字符串分析成数组,再次确保有page选项  

        parse_str($parse['query'],$parms);  

  

        if (!array_key_exists('page', $parms)) {  

            $parms['page'] = $this->curr;  

        }  

  

        //上边四种情况都测试一遍,page参数都能生成  

        //print_r($parms);  

  

        //判断除了page之外,还有没有其他参数  

  

        if (count($parms) == 1) {  

            $url = $parse['path'] . '?';  

        } else {  

            unset($parms['page']);  

            $url = $parse['path'] . '?' . http_build_query($parms) . '&';  

        }  

  

        //echo $url  

        $prev = $this->curr - 1;  

        $next = $this->curr + 1;  

  

        //首页  

        $indexLink = '首页';  

  

        //上一页  

        if ($prev

            $prevLink = '';  

        }else {  

            $prevLink = '上一页';  

        }  

  

        //下一页  

        if ($next > $cnt) {  

            $nextLink = '';  

        }else {  

            $nextLink = '下一页';  

        }  

        //尾页  

        $lastLink = '尾页';  

  

        //echo $indexLink.'  '.$prevLink.'  '.$nextLink .'  '.$lastLink;  

        //上一页,1 2 3 4 5 下一页  

  

        $start = $this->curr - (5-1)/2; //计算左侧开始的页码  

        $end = $this->curr + (5-1)/2;    //计算右侧开始的页码  

          

        //如果左侧的页面,已经小于1,则把小于1 的部分补到右侧  

        if ($start

            $end += (1 - $start);  

            $start = 1; //修改start = 1  

          

            if ($end > $cnt) {  

                $end  = $cnt;  

            }  

        }  

  

        //把右侧超出的部分,补到左边  

        if ($end > $cnt) {  

            $start -= ($end - $cnt);  

            $end = $cnt;  

  

            if ($start

                $start = 1;  

            }  

        }  

          

  

        //循环出页码数  

        $pageStr = '';  

        for ($i=$start; $i

                  

            if ($i == $this->curr) {  

                $pageStr .= $i;  

                continue;  

            }  

  

            $pageStr .= '' . $i . '';  

        }  

        return $indexLink.$prevLink.$pageStr.$nextLink.$lastLink;  

    }  

  

  

  

}  

  

  

$page = new Page(30,3);  

echo $page->showPage();  

Stellungnahme:
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