分頁類,php分頁類_PHP教程

來源:互聯網
上載者:User

分頁類,php分頁類


page.class.php

  1 php   2 /*   3 * PHP分頁類   4 * @package Page   5 * @Created 2013-03-27   6 * @Modify 2013-03-27   7 * @link http://www.60ie.net   8 * Example:   9 $myPage=new Pager(1300,intval($CurrentPage));  10 $pageStr= $myPage->GetPagerContent();  11 echo $pageStr;  12 */  13 class Pager {  14 private $pageSize = 10;  15 private $pageIndex; //頁碼 16 private $totalNum; //資料總數 17  18 private $totalPagesCount; //總頁數 19  20 private $pageUrl; // 21 private static $_instance; 22  23 public function __construct($p_totalNum, $p_pageIndex, $p_pageSize = 10,$p_initNum=3,$p_initMaxNum=5) {  24 if (! isset ( $p_totalNum ) || !isset($p_pageIndex)) {  25 die ( "pager initial error" );  26 } 27  28 $this->totalNum = $p_totalNum; //資料總數 29 $this->pageIndex = $p_pageIndex;  30 $this->pageSize = $p_pageSize;  31 $this->initNum=$p_initNum;  32 $this->initMaxNum=$p_initMaxNum;  33 $this->totalPagesCount= ceil($p_totalNum / $p_pageSize);  34 $this->pageUrl=$this->_getPageUrl(); 35  36 $this->_initPagerLegal();  37 } 38  39  40 /**  41 * 擷取去除page部分的當前URL字串  42 *  43 * @return String URL字串  44 */  45 private function _getPageUrl() {  46 $CurrentUrl = $_SERVER["REQUEST_URI"]; //擷取URI 47 $arrUrl = parse_url($CurrentUrl); //解析URI,返回一關聯陣列 48 $urlQuery = $arrUrl["query"]; //擷取數組鍵為query的值 49 //將URI中的“?page=頁碼”置換為“?page” 50 if($urlQuery){  51 //$urlQuery = preg_replace("/(^|&)page=".$this->pageIndex."/", "", $urlQuery); 52 $urlQuery = ""; 53 $CurrentUrl = str_replace($arrUrl["query"], $urlQuery, $CurrentUrl); 54 echo $CurrentUrl; 55 if($urlQuery){  56 $CurrentUrl.="&page";  57 }else{ 58 $CurrentUrl.="page";  59 } 60 } else {  61 $CurrentUrl.="?page";  62 } 63  64 return $CurrentUrl; 65  66 }  67 /*  68 *設定頁面參數合法性  69 *@return void  70 */  71 private function _initPagerLegal()  72 {  73 //頁碼不合法時,修正頁碼 74 if((!is_numeric($this->pageIndex)) || $this->pageIndex<1)  75 {  76 $this->pageIndex=1;  77 }elseif($this->pageIndex > $this->totalPagesCount)  78 {  79 $this->pageIndex=$this->totalPagesCount;  80 }  81 }  82  83 public function GetPagerContent() {  84 $str = "";  85 //首頁 上一頁  86 /*if($this->pageIndex==1)  87 {  88 $str .="首頁 "."\n";  89 $str .="上一頁 "."\n"."\n";  90 }else  91 {  92 $str .="pageUrl}=1' class='tips' title='首頁'>首頁 "."\n";  93 $str .="pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一頁'>上一頁 "."\n"."\n";  94 } 95 */ 96 if($this->pageIndex!=1){ 97 $str .="$this->pageUrl}=1' class='tips' title='首頁'>首頁 "."\n";  98 $str .="$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一頁'>上一頁 "."\n"."\n"; 99 }100 /*101 102 除首末後 頁面分頁邏輯103 104 */ 105 //10頁(含)以下 106 $currnt=""; 107 if($this->totalPagesCount<=10) 108 {109 110 for($i=1;$i<=$this->totalPagesCount;$i++)111 112 { 113 if($i==$this->pageIndex) 114 { $currnt=" class='current'";} 115 else 116 { $currnt=""; } 117 $str .="$this->pageUrl}={$i} ' {$currnt}>$i"."\n" ; 118 } 119 }else //10頁以上 120 { if($this->pageIndex<3) //當前頁小於3 121 { 122 for($i=1;$i<=3;$i++) 123 { 124 if($i==$this->pageIndex) 125 { $currnt=" class='current'";} 126 else 127 { $currnt=""; } 128 $str .="$this->pageUrl}={$i} ' {$currnt}>$i"."\n" ; 129 }130 131 $str.="……"."\n";132 133 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1 134 { 135 $str .="$this->pageUrl}={$i}' >$i"."\n" ;136 137 } 138 }elseif($this->pageIndex<=5) // 5 >= 當前頁 >= 3 139 { 140 for($i=1;$i<=($this->pageIndex+1);$i++) 141 { 142 if($i==$this->pageIndex) 143 { $currnt=" class='current'";} 144 else 145 { $currnt=""; } 146 $str .="$this->pageUrl}={$i} ' {$currnt}>$i"."\n" ;147 148 } 149 $str.="……"."\n";150 151 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1 152 { 153 $str .="$this->pageUrl}={$i}' >$i"."\n" ;154 155 }156 157 }elseif(5<$this->pageIndex && $this->pageIndex<=$this->totalPagesCount-5 ) //當前頁大於5,同時小於總頁數-5158 159 {160 161 for($i=1;$i<=3;$i++) 162 { 163 $str .="$this->pageUrl}={$i}' >$i"."\n" ; 164 } 165 $str.="……"; 166 for($i=$this->pageIndex-1 ;$i<=$this->pageIndex+1 && $i<=$this->totalPagesCount-5+1;$i++) 167 { 168 if($i==$this->pageIndex) 169 { $currnt=" class='current'";} 170 else 171 { $currnt=""; } 172 $str .="$this->pageUrl}={$i} ' {$currnt}>$i"."\n" ; 173 } 174 $str.="……";175 176 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++) 177 { 178 $str .="$this->pageUrl}={$i}' >$i"."\n" ;179 180 } 181 }else 182 {183 184 for($i=1;$i<=3;$i++) 185 { 186 $str .="$this->pageUrl}={$i}' >$i"."\n" ; 187 } 188 $str.="……"."\n";189 190 for($i=$this->totalPagesCount-5;$i<=$this->totalPagesCount;$i++)//功能1 191 { 192 if($i==$this->pageIndex) 193 { $currnt=" class='current'";} 194 else 195 { $currnt=""; } 196 $str .="$this->pageUrl}={$i} ' {$currnt}>$i"."\n" ;197 198 } 199 }200 201 }202 203  204 205 206 /*207 208 除首末後 頁面分頁邏輯結束209 210 */211 212 //下一頁 末頁 213 /*if($this->pageIndex==$this->totalPagesCount) 214 { 215 $str .="\n"."下一頁"."\n" ; 216 $str .="末頁"."\n"; 217 }else 218 { 219 $str .="\n"."pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一頁'>下一頁 "."\n"; 220 $str .="pageUrl}={$this->totalPagesCount}' class='tips' title='末頁'>末頁 "."\n" ; 221 } 222 */223 if($this->pageIndex!=$this->totalPagesCount){224 $str .="\n"."$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一頁'>下一頁 "."\n"; 225 $str .="$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末頁'>末頁 "."\n" ;226 }227 $str .= ""; 228 return $str; 229 }230 231  232 233 234 /** 235 * 獲得執行個體 236 * @return 237 */ 238 // static public function getInstance() { 239 // if (is_null ( self::$_instance )) { 240 // self::$_instance = new pager (); 241 // } 242 // return self::$_instance; 243 // }244 245 246 } 247 ?>

實作類別page.php

 1      2      3 ----分頁示範-----     4  5      6      7     php     8      include "pager.class.php";     9      $CurrentPage=isset($_GET['page'])?$_GET['page']:1;      10      $myPage=new pager(1300,intval($CurrentPage));    11       $pageStr= $myPage->GetPagerContent();  12      echo $pageStr;    13     ?>    14     15    

樣式設定

 1 body,html{ padding:0px; margin:0px; color:#333333; font-family:"宋體",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px; line-height:150%;}     2  3 h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}     4  5 /**前台分頁樣式**/    6  7 .Pagination {margin:10px 0 0;padding:5px 0;text-align:rightright; height:20px; line-height:20px; font-family:Arial, Helvetica, sans-serif,"宋體";}     8  9 .Pagination a {margin-left:2px;padding:2px 7px 2px;}    10 11 .Pagination .dot{ border:medium none; padding:4px 8px}    12 13 .Pagination a:link, .Pagination a:visited {border:1px solid #dedede;color:#696969;text-decoration:none;}    14 15 .Pagination a:hover, .Pagination a:active, .Pagination a.current:link, .Pagination a.current:visited {border:1px solid #dedede;color:#fff; background-color:#ff6600; background-image:none; border:#ff6600 solid 1px;}    16 17 .Pagination .selectBar{ border:#dedede solid 1px; font-size:12px; width:95px; height:21px; line-height:21px; margin-left:10px; display:inline}    18 19 .Pagination a.tips{_padding:4px 7px 1px;}   

實現效果

http://www.bkjia.com/PHPjc/977084.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/977084.htmlTechArticle分頁類,php分頁類 page.class.php 1 ? php 2 /* 3 * PHP分頁類 4 * @package Page 5 * @Created 2013-03-27 6 * @Modify 2013-03-27 7 * @link http://www.60ie.net 8 * Example: 9...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.