Home  >  Article  >  php教程  >  PHP生成分页

PHP生成分页

PHP中文网
PHP中文网Original
2016-05-26 08:19:05978browse

一个非常好用的php生成分页的封装类

<?php 
class Page {
    private $total;
    private $pagesize;
    private $page;
    private $url;
    function Page($total,$pagesize,$page) {
         $this->total=$total;
         $this->pagesize=$pagesize;
         $this->page=$page;
         $this->url=$this->pageurl();
         
    }
    private function pageurl() {
         $url=$_SERVER["REQUEST_URI"];
         $url=eregi_replace(&#39;\?page=[0-9]+&#39;,&#39;&#39;,$url);
         $url=eregi_replace(&#39;\&page=[0-9]+&#39;,&#39;&#39;,$url);
         if(strpos ($url, "?")=="")
         { $url="".$url."?";}
         else{ $url="".$url."&";}
         Return $url;    
    }
    function pagehtml() {
        $count=$this->total/$this->pagesize;
        if(is_int($count)) {
            $pagecount=$count;
             
        }else {
            $pagecount=(int)$count+1;
        }
        $html=&#39;<style type="text/css">.pager{margin:0; font-size:12px;}.pager span{ color:#FFF; background:#666;padding:2px 6px;margin:0 5px;border:1px solid #666;font:normal 12px/28px "宋体"; }.pager a{padding:2px 6px;background-color:#FFF; margin:0 5px; border:1px solid #666;color:#666; text-decoration:none;font:normal 12px/25px "宋体";}.pager a:hover{ padding:2px 6px;background-color:#FFF; margin:0 5px; border:1px solid #666;color:#FFF; background:#666;font:normal 12px/25px "宋体"; }.pager form select{ font:normal 13px/28px "宋体";}</style>&#39;;
        $html.="<form><p class=&#39;pager&#39;>总计: ".$this->total.", 每页:".$this->pagesize.", 页数: ".$this->page.&#39;/&#39;.$pagecount.", ";
        if($this->page-3>1) {
            $pagestar=$this->page-3;
        }else {
            $pagestar=1;
        }
         
        if($this->page+3<$pagecount)  
        {
            $pageend=$this->page+3;
        }else{
            $pageend=$pagecount;
        }
        if($this->page>1) {
            $html.=&#39;<a href=&#39;.$this->url.&#39;page=1 >首 页</a>&#39;;
            $html.=&#39;<a href=&#39;.$this->url.&#39;page=&#39;.($this->page-1).&#39;>上一页</a>&#39;;
        }
        else {
            $html.=&#39;<span>首 页</span>&#39;;
            $html.=&#39;<span>上一页</span>&#39;;
             
        }
        for($ii=$pagestar; $ii<=$pageend; $ii++) {
            if($ii<>$this->page) {
            $html.=&#39;<a href=&#39;.$this->url.&#39;page=&#39;.$ii.&#39;>&#39;.$ii.&#39;</a>&#39;; 
            }
             else {
                $html.=&#39;<span>&#39;.$ii.&#39;</span>&#39;;
             }
        }
        if($this->page<$pagecount) {
             
            $html.=&#39;<a href=&#39;.$this->url.&#39;page=&#39;.($this->page+1).&#39;>下一页</a>&#39;;
            $html.=&#39;<a href=&#39;.$this->url.&#39;page=&#39;.$pagecount.&#39; >末 页</a>&#39;;
        }
        else {
            $html.=&#39;<span>下一页</span>&#39;;
            $html.=&#39;<span>末 页</span>&#39;;
             
        }
        $html.=" <select name=&#39;pa&#39; onchange=&#39;javascript:window.location.href=this.options[this.selectedIndex].value&#39;>";
        for($j=1;$j<=$pagecount;$j++)
        {
             $html.="<option  value=&#39;".$this->url."page=".$j."&#39;";
             if($j==$this->page) {
                $html.=&#39; selected &#39;;    
             }
             $html.=">".$j."</option>";
        }
        $html.="</select></p></form>";
         echo($html);
             
        }
}
  
?>
Statement:
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