时间:2021-07-01 10:21:17 帮助过:5人阅读
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,同时小于总页数-5 158 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