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 = "<div class=\"Pagination\">";
85 //首頁 上一頁
86 /*if($this->pageIndex==1)
87 {
88 $str .="<a href='javascript:void(0)' class='tips' title='首頁'>首頁</a> "."\n";
89 $str .="<a href='javascript:void(0)' class='tips' title='上一頁'>上一頁</a> "."\n"."\n";
90 }else
91 {
92 $str .="<a href='{$this->pageUrl}=1' class='tips' title='首頁'>首頁</a> "."\n";
93 $str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一頁'>上一頁</a> "."\n"."\n";
94 }
95 */
96 if($this->pageIndex!=1){
97 $str .="<a href='{$this->pageUrl}=1' class='tips' title='首頁'>首頁</a> "."\n";
98 $str .="<a href='{$this->pageUrl}=".($this->pageIndex-1)."' class='tips' title='上一頁'>上一頁</a> "."\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 .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\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 .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;
129 }
130
131 $str.="<span class=\"dot\">……</span>"."\n";
132
133 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1
134 {
135 $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\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 .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;
147
148 }
149 $str.="<span class=\"dot\">……</span>"."\n";
150
151 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)//功能1
152 {
153 $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;
154
155 }
156
157 }elseif(5<$this->pageIndex && $this->pageIndex<=$this->totalPagesCount-5 ) //當前頁大於5,同時小於總頁數-5
158
159 {
160
161 for($i=1;$i<=3;$i++)
162 {
163 $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;
164 }
165 $str.="<span class=\"dot\">……</span>";
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 .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\n" ;
173 }
174 $str.="<span class=\"dot\">……</span>";
175
176 for($i=$this->totalPagesCount-3+1;$i<=$this->totalPagesCount;$i++)
177 {
178 $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;
179
180 }
181 }else
182 {
183
184 for($i=1;$i<=3;$i++)
185 {
186 $str .="<a href='{$this->pageUrl}={$i}' >$i</a>"."\n" ;
187 }
188 $str.="<span class=\"dot\">……</span>"."\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 .="<a href='{$this->pageUrl}={$i} ' {$currnt}>$i</a>"."\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"."<a href='javascript:void(0)' class='tips' title='下一頁'>下一頁</a>"."\n" ;
216 $str .="<a href='javascript:void(0)' class='tips' title='末頁'>末頁</a>"."\n";
217 }else
218 {
219 $str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一頁'>下一頁</a> "."\n";
220 $str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末頁'>末頁</a> "."\n" ;
221 }
222 */
223 if($this->pageIndex!=$this->totalPagesCount){
224 $str .="\n"."<a href='{$this->pageUrl}=".($this->pageIndex+1)."' class='tips' title='下一頁'>下一頁</a> "."\n";
225 $str .="<a href='{$this->pageUrl}={$this->totalPagesCount}' class='tips' title='末頁'>末頁</a> "."\n" ;
226 }
227 $str .= "</div>";
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 <head> 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 3 <title>----分頁演示-----</title> 4 <link href="pager.css" type="text/css" rel="stylesheet" /> 5 </head> 6 <body> 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 </body> 15 </html>
樣式設置
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;}
實現效果
