一个分页类

<?php

class page {

    private $Now_Page;//当前页面值
    private $total;//总页面数
    private $Page_num;//每页记录总数
    private $Page_count;//总页面数
    private $Url_css;//URI的css样式
    private $noUrl_css;//非URI的css样式
    private $Pre_page;//前一页
    private $Next_page;//后一页
    private $Last_page;//尾页

   //构造函数传递总页数和当前页数非当前页面css样式及当前页面css样式
   function __construct($total ,$page ,$nums=10 ,$Url_class=null ,$noUrl_css=null) {

             $this->total = $total;
             $this->Now_Page = $page;
             $this->Page_num = $nums;
             $this->Url_css = $Url_class;
             $this->noUrl_css = $noUrl_css;
   }

   //获得页面URI,$page为当前页面传递的页面值,$var 为传递页面的参数字符串
    private function Get_URI($var="page") {
            $page = $this->Now_Page;
            $url = $_SERVER["REQUEST_URI"];
   $url_parse = parse_url($url);
            $url_query = $url_parse['query'];
   $query = $url_query;

   if($url_query){
     $url_query = ereg_replace("(^|&)$var=$page","",$url_query);
     $url = ereg_replace($query, $url_query, $url);
     if($url_query){
     $url.= "&$var";
     }//if
     else{
     $url.="$var";
     }//else
   }//if
   else{
   $url.="?$var";
   }
            return $url;
 }

    //获得页面总数以及前一页、后一页、最后一页
 private function Page_counts(){

       $total = $this->total;
       if($total<1)
         return false;
       $page_count = ceil($this->total/$this->Page_num);
       $this->Page_count = $page_count;
       $this->Pre_page = $this->Now_Page>1?($this->Now_Page-1):false;
       $this->Next_page = $this->Now_Page<$page_count?($this->Now_Page+1):false;
       $this->Last_page = $this->Page_count;
       return true;
 }

    //判断是否无记录,有记录返回false
    private function No_nums() {

        if(!$this->Page_counts()){
    echo "很抱歉,暂时没有记录";
    echo '点击<a href="#" οnclick="history.go(-1)">这里</a>返回';
        }
        else
        return false;
    }

 //输出类型
 public function Print_page() {

        if(!$this->No_nums())//无记录
          $this->Print_style1();

 }
 //输出样式一
   private function Print_style1() {

          $url = $this->Get_URI();
    echo  '<span class="'.$this->noUrl_css.'">共'.$this->Page_count.'页</span>';
    echo '&nbsp;&nbsp;&nbsp;&nbsp;';
          if($this->Pre_page)
          echo '<a href="'.$url.'='.$this->Pre_page.'" class="'.$this->Url_css.'"> < </a>';
          else
             echo '<span class="'.$this->noUrl_css.'"> < </span>';
          if($this->Page_count<11){//总页数小于11
        for($i=1;$i<=$this->Page_count;$i++){
        if($i==$this->Now_Page){
           echo '<span class="'.$this->noUrl_css.'">'.$i.'</span>';
          }else{
            echo '<a href="'.$url.'='.$i.'" class="'.$this->Url_css.'">'.$i.'</a>';
          }
        }//for
   }//if
   else{//总页数大于十一
     if($this->Now_Page<=10){//如果当前页面不大于10则从第一页显示
      for($i=1;$i<11;$i++){
        if($i==$this->Now_Page){
            echo '<span class="'.$this->noUrl_css.'">'.$i.'</span>';
          }
          else{
            echo '<a href="'.$url.'='.$i.'" class="'.$this->Url_css.'">'.$i.'</a>';
          }
       }
     }
     else{//如果当前页面大于10则从第十一页显示
          $j=(($this->Now_Page-1)/10);
          settype($j,"integer");
          $i=$j*10; $j=$i+11;

         if($j>$this->Page_count){
         $j=$this->Page_count+1;
         }
       for(++$i;$i<$j;$i++){
         if($i==$this->Now_Page){
          echo '<span class="'.$this->noUrl_css.'">'.$i.'</span>';
           }else{
         echo '<a href="'.$url.'='.$i.'" class="'.$this->Url_css.'">'.$i.'</a>';
           }
          }//for
     }
       }//else

   if($this->Next_page){
   echo '<a href="'.$url.'='.$this->Next_page.'" class="'.$this->Url_css.'"> > </a>';
            }else{
       echo '<span class="'.$this->noUrl_css.'"> > </span>';
   }

 

 

   }

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值