個人學習php所寫簡單分頁類,通用性比較強
-
/**
- * 簡單分頁類
- * @author:phpprince
- * @QQ: 8923052
- * @date: 2014-04-22 21:08:35
- */
- class Page{
- protected $url; //位址
- protected $allnum; / /總記錄數
- protected $current; //目前頁碼
- protected $pagesize; //每頁顯示多少筆記錄
- protected $postfix; //後字
- protected $style; //顯示樣式共3種樣式,1 2 3 分別表示前5後4,前3後3,前4後4
-
- public function __construct($url,$allnum,$current,$pagesize=10,$ postfix='',$style=1){
- $this->url=$url;
- $this->allnum=$allnum;
- $this->current=$current;
- $this->pagesize=$pagesize;
- $this->postfix=$postfix;
- $this->style=$style;
- }
-
- //取得總頁數
- protected function maxPageNum(){
- $max=ceil($this->allnum/$this->pagesize);
- //頁碼超限校正
- if($this->current>$this->current>$this->current>$this->current>$this->current>$ max){
- $this->current=$max;
- }
- if($this->current $this->current=1;
- }
- return $max;
- }
-
- //取得第一頁連結完整html str
- protected function firstUrl(){
- if($this->current!=1)
- {
- return '首頁';
- }else{
- return '首頁';
- }
- }
-
- //取得上一頁連結完整html str
- protected function prevUrl(){
- if($this->current $fullurl='上一頁';
- }else{
- $fullurl=$this->url.($this->current-1).$this->postfix;
- $fullurl='上頁';
- }
- return $fullurl;
- }
-
- //取得下一頁連結完整html str
- protected function nextUrl(){
- if($this->current>= $this->maxPageNum()){
- $fullurl='下一頁';
- }else{
- $ fullurl=$this->url.($this->current 1).$this->postfix;
- $fullurl='下一頁';
- }
- return $fullurl;
- }
-
- //取得最後一頁鏈接完整html str
- protected function lastUrl(){
- if($this->current>=$this->maxPageNum()){
- $fullurl='末頁';
- }else{
- $fullurl=$this->url.$this->maxPageNum().$this->postfix;
- $fullurl ='末頁';
- }
- return $fullurl;
- }
-
- //取得指定頁碼完整url
- protected function getUrl($pageno){
- return $this->url.$pageno.$this->postfix;
- }
-
- //指定顯示樣式
- protected function getStyle(){
- switch($this->style){
- case 1:
- $before=5;
- $after=4;
- break;
- case 2:
- $before=3;
- $after=3;
- break;
- case 3:
- $ before=4;
- $after=4;
- break;
- default :
- $before=5;
- $after=4;
- }
-
- return array ($before,$after);
- }
-
- //取得中間URL 1 2 3 4 5 ⑥ 7 8 9 10
- protected function getMiddelUrl(){
- $max=$this ->maxPageNum(); //先取得總頁,可校正目前頁超限問題
- $current=$this->current; //目前頁碼必須合法,才能保證下面的頁碼範圍一定正確
- //得到目前樣式
- list($before,$after)=$this->getStyle();
- $startno=$current-$before; //起始頁碼
- $endno=$current $after; //終止頁碼
-
- //為保證輸出始終符合要求,在頁面不超限前提下,起始頁增加,則終止頁必須增加,反之同理.
- while( $endno>$max||$startno if($endno>$max){ //終止頁碼超界
- $endno--;
- if($startno>1){
- $startno--;
- }
- }
- if($startno $startno ;
- if($endno $endno ;
- }
- }
- }
- $str=''; //用來儲存整個html str
- for($i=$startno;$i $currenturl=$ this->getUrl($i);
- if($i!=$current){
- $str.="{$i}" ;
- }else{
- $str.=''.$i.'';
- }
- }
- return $str;
- }
-
- //回傳完整分頁html字串
- public function getPageStr(){
- $str='
' .$this->firstUrl().$this->prevUrl(); - $str.=$this->getMiddelUrl();
- $str.=$this->nextUrl().$this- >lastUrl().'共'.$this->maxPageNum().'頁'.$this->allnum.'條
';
- return $str;
- }
- }
- ?>
複製程式碼
|