首页 > 代码库 > PHP---分页类(page.class.php)

PHP---分页类(page.class.php)

  1 <?php
  2 class  Page
  3 {
  4     private $total;      //数据总记录数
  5     private $listRows;   //每页显示的行数
  6     private $limit;      //偏移量长度
  7     private $url;
  8     private $pageNum;     //总页数
  9     private $config=array(‘header‘=>‘个记录‘,‘prev‘=>‘上一页‘,‘next‘=>‘下一页‘,‘first‘=>‘首页‘,‘last‘=>‘末页‘);
 10     private $listNum=8;   //限制页码数目
 11 
 12     public function __construct($total,$listRows=10,$path="")
 13     {
 14            $this->total=$total;
 15            $this->listRows=$listRows;
 16            $this->url=$this->getUrl($path);
 17            $this->page=!empty($_GET["page"])?$_GET["page"]:1;
 18            $this->pageNum=ceil($this->total/$this->listRows);
 19            $this->limit=$this->setLimit();
 20     }
 21 
 22     private function setLimit()
 23     {
 24         return "Limit ".($this->page-1)*$this->listRows.",{$this->listRows}";
 25     }
 26     
 27     private function getUrl($path)
 28     {
 29         $url=$_SERVER[‘REQUEST_URL‘].(strpos($_SERVER[‘REQUEST_URL‘], ‘?‘)?‘‘:"?").$path;
 30         $parse=parse_url($url);
 31 
 32         if (isset($parse[‘query‘])) {
 33             parse_str($parse[‘query‘],$parmas);
 34             unset($parmas["page"]);
 35             $url=$parse[‘path‘].‘?‘.http_build_query($parmas);
 36         }
 37         return $url;
 38     }
 39     
 40   
 41     function __get($args)
 42     {
 43         if ($args=="limit") {
 44             return $this->limit;
 45         }
 46         else
 47         {
 48             return null;
 49         }
 50 
 51     }
 52   
 53   private function start()
 54   {
 55       if ($this->total==0) {
 56           return 0;
 57       }
 58       else
 59       {
 60           return ($this->page-1)*$this->listRows+1;
 61       }
 62   }
 63    
 64    private function end()
 65    {
 66        return min($this->page*$this->listRows,$this->total);
 67    }
 68 
 69    private function first()
 70    {
 71      $html="";
 72      if($this->page==1)
 73      {
 74          $html.="";
 75      }
 76      else
 77      {
 78          $html.="&nbsp;&nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page=1‘>{$this->config[‘first‘]}</a>&nbsp;&nbsp";
 79      }
 80      return $html;
 81    }
 82 
 83    private function prev()
 84    {
 85        $html="";
 86         if($this->page==1)
 87      {
 88          $html.="";
 89      }
 90      else
 91      {
 92          $html.="&nbsp;&nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page=".($this->page-1)."‘>{$this->config[‘prev‘]}</a>&nbsp;&nbsp";
 93      }
 94      return $html;
 95    }
 96 
 97   private function pageList()
 98   {
 99       $linkPage="";
100       $inum=floor($this->listNum/2);
101       for ($i=$inum; $i>=1; $i--) 
102       {
103        $page=$this->page-$i;
104        if($page<1)
105        {
106            continue;
107        }
108        $linkPage.="$nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page={$page}‘>{$page}</a>&nbsp;";
109       }
110 
111 
112       $linkPage.="&nbsp;{$this->page}&nbsp;";
113 
114       for ($i=1; $i <$inum ; $i++) { 
115           $page=$this->page+$i;
116           if ($page<=$this->pageNum) {
117               $linkPage.="&nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page={$page}‘>{$page}</a>&nbsp;";
118           }
119           else
120           {
121               break;
122           }
123       }
124       return $linkPage;
125   }
126 
127 
128   private function next(){
129           $html="";
130         if($this->page==$this->pageNum)
131      {
132          $html.="";
133      }
134      else
135      {
136          $html.="&nbsp;&nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page=".($this->page+1)."‘>{$this->config[‘next‘]}</a>&nbsp;&nbsp";
137      }
138      return $html;
139   }
140 
141 private function last(){
142           $html="";
143         if($this->page==$this->pageNum)
144      {
145          $html.="";
146      }
147      else
148      {
149          $html.="&nbsp;&nbsp;<a href=http://www.mamicode.com/‘{$this->url}&page=".($this->pageNum)."‘>{$this->config[‘last‘]}</a>&nbsp;&nbsp";
150      }
151      return $html;
152   }
153 
154    private function showPage($display=array(0,1,2,3,4,5,6,7))
155    {
156       $html[0]="&nbsp;&nbsp;共有<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;";
157       $html[1]="&nbsp;&nbsp;每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条&nbsp;&nbsp;";
158       $html[2]="&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;";
159       $html[3]=$this->first();
160       $html[4]=$this->prev();
161       $html[5]=$this->pageList();
162       $html[6]=$this->next();
163       $html[7]=$this->last();
164       $fpage="";
165       foreach ($display as  $index) {
166           $fpage.=$html[$index];
167       }
168       return $fpage;
169    }
170 
171 }
172 
173 
174 
175 ?>

 

PHP---分页类(page.class.php)