php分页类

  1 <?php

  2 namespace Components;

  3 

  4 class Page {

  5     private $total; //数据表中总记录数

  6     private $listRows; //每页显示行数

  7     private $limit;

  8     private $uri;

  9     private $pageNum; //页数

 10     private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");

 11     private $listNum=8;

 12     /*

 13      * $total

 14      * $listRows

 15      */

 16     public function __construct($total, $listRows=10, $pa=""){

 17         $this->total=$total;

 18         $this->listRows=$listRows;

 19         $this->uri=$this->getUri($pa);

 20         $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;

 21         $this->pageNum=ceil($this->total/$this->listRows);

 22         $this->limit=$this->setLimit();

 23     }

 24 

 25     private function setLimit(){

 26         return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";

 27     }

 28 

 29     private function getUri($pa){

 30         $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;

 31         $parse=parse_url($url);

 32 

 33 

 34 

 35         if(isset($parse["query"])){

 36             parse_str($parse['query'],$params);

 37             unset($params["page"]);

 38             $url=$parse['path'].'?'.http_build_query($params);

 39 

 40         }

 41 

 42         return $url;

 43     }

 44 

 45     function __get($args){

 46         if($args=="limit")

 47             return $this->limit;

 48         else

 49             return null;

 50     }

 51 

 52     private function start(){

 53         if($this->total==0)

 54             return 0;

 55         else

 56             return ($this->page-1)*$this->listRows+1;

 57     }

 58 

 59     private function end(){

 60         return min($this->page*$this->listRows,$this->total);

 61     }

 62 

 63     private function first(){

 64         $html = "";

 65         if($this->page==1)

 66             $html.='';

 67         else

 68             $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;";

 69 

 70         return $html;

 71     }

 72 

 73     private function prev(){

 74         $html = "";

 75         if($this->page==1)

 76             $html.='';

 77         else

 78             $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;&nbsp;";

 79 

 80         return $html;

 81     }

 82 

 83     private function pageList(){

 84         $linkPage="";

 85 

 86         $inum=floor($this->listNum/2);

 87 

 88         for($i=$inum; $i>=1; $i--){

 89             $page=$this->page-$i;

 90 

 91             if($page<1)

 92                 continue;

 93 

 94             $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";

 95 

 96         }

 97 

 98         $linkPage.="&nbsp;{$this->page}&nbsp;";

 99 

100 

101         for($i=1; $i<=$inum; $i++){

102             $page=$this->page+$i;

103             if($page<=$this->pageNum)

104                 $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";

105             else

106                 break;

107         }

108 

109         return $linkPage;

110     }

111 

112     private function next(){

113         $html = "";

114         if($this->page==$this->pageNum)

115             $html.='';

116         else

117             $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;&nbsp;";

118 

119         return $html;

120     }

121 

122     private function last(){

123         $html = "";

124         if($this->page==$this->pageNum)

125             $html.='';

126         else

127             $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;&nbsp;";

128 

129         return $html;

130     }

131 

132     private function goPage(){

133         return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'">&nbsp;&nbsp;';

134     }

135     function fpage($display=array(0,1,2,3,4,5,6,7,8)){

136         $html[0]="&nbsp;&nbsp;共有<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;";

137         $html[1]="&nbsp;&nbsp;每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条&nbsp;&nbsp;";

138         $html[2]="&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;";

139 

140         $html[3]=$this->first();

141         $html[4]=$this->prev();

142         $html[5]=$this->pageList();

143         $html[6]=$this->next();

144         $html[7]=$this->last();

145         $html[8]=$this->goPage();

146         $fpage='';

147         foreach($display as $index){

148             $fpage.=$html[$index];

149         }

150 

151         return $fpage;

152 

153     }

154 

155 

156     }

 

你可能感兴趣的:(PHP)