两款万能的php分页类
第一款php分页类
1 <?php 2 /* 3 * To change this template, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7 /** 8 * 分页类 9 * 使用方式: 10 * $page = new Page(); 11 * $page->init(1000, 20); 12 * $page->setNotActiveTemplate('<span> {a} </span>'); 13 * $page->setActiveTemplate('{a}'); 14 * echo $page->show(); 15 * 16 * 17 * @author 风居住的地方 18 */ 19 class Page { 20 /** 21 * 总条数 22 */ 23 private $total; 24 /** 25 * 每页大小 26 */ 27 private $pageSize; 28 /** 29 * 总页数 30 */ 31 private $pageNum; 32 /** 33 * 当前页 34 */ 35 private $page; 36 /** 37 * 地址 38 */ 39 private $uri; 40 /** 41 * 分页变量 42 */ 43 private $pageParam; 44 /** 45 * LIMIT XX,XX 46 */ 47 private $limit; 48 /** 49 * 数字分页显示 50 */ 51 private $listnum = 8; 52 /** 53 * 分页显示模板 54 * 可用变量参数 55 * {total} 总数据条数 56 * {pagesize} 每页显示条数 57 * {start} 本页开始条数 58 * {end} 本页结束条数 59 * {pagenum} 共有多少页 60 * {frist} 首页 61 * {pre} 上一页 62 * {next} 下一页 63 * {last} 尾页 64 * {list} 数字分页 65 * {goto} 跳转按钮 66 */ 67 private $template = '<div><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></div>'; 68 /** 69 * 当前选中的分页链接模板 70 */ 71 private $activeTemplate = '<li class="active"><a href="javascript:;">{text}</a></li>'; 72 /** 73 * 未选中的分页链接模板 74 */ 75 private $notActiveTemplate = '<li><a href="{url}">{text}</a></li>'; 76 /** 77 * 显示文本设置 78 */ 79 private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页'); 80 /** 81 * 初始化 82 * @param type $total 总条数 83 * @param type $pageSize 每页大小 84 * @param type $param url附加参数 85 * @param type $pageParam 分页变量 86 */ 87 public function init($total, $pageSize, $param = '', $pageParam = 'page') { 88 $this->total = intval($total); 89 $this->pageSize = intval($pageSize); 90 $this->pageParam = $pageParam; 91 $this->uri = $this->geturi($param); 92 $this->pageNum = ceil($this->total / $this->pageSize); 93 $this->page = $this->setPage(); 94 $this->limit = $this->setlimit(); 95 } 96 97 /** 98 * 设置分页模板 99 * @param type $template 模板配置 100 */ 101 public function setTemplate($template) { 102 $this->template = $template; 103 } 104 105 /** 106 * 设置选中分页模板 107 * @param type $activeTemplate 模板配置 108 */ 109 public function setActiveTemplate($activeTemplate) { 110 $this->activeTemplate = $activeTemplate; 111 } 112 113 /** 114 * 设置未选中分页模板 115 * @param type $notActiveTemplate 模板配置 116 */ 117 public function setNotActiveTemplate($notActiveTemplate) { 118 $this->notActiveTemplate = $notActiveTemplate; 119 } 120 121 /** 122 * 返回分页 123 * @return type 124 */ 125 public function show() { 126 return str_ireplace(array( 127 '{total}', 128 '{pagesize}', 129 '{start}', 130 '{end}', 131 '{pagenum}', 132 '{frist}', 133 '{pre}', 134 '{next}', 135 '{last}', 136 '{list}', 137 '{goto}', 138 ), array( 139 $this->total, 140 $this->setPageSize(), 141 $this->star(), 142 $this->end(), 143 $this->pageNum, 144 $this->frist(), 145 $this->prev(), 146 $this->next(), 147 $this->last(), 148 $this->pagelist(), 149 $this->gopage(), 150 ), $this->template); 151 } 152 153 /** 154 * 获取limit起始数 155 * @return type 156 */ 157 public function getOffset() { 158 return ($this->page - 1) * $this->pageSize; 159 } 160 161 /** 162 * 设置LIMIT 163 * @return type 164 */ 165 private function setlimit() { 166 return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}"; 167 } 168 169 /** 170 * 获取limit 171 * @param type $args 172 * @return type 173 */ 174 public function __get($args) { 175 if ($args == "limit") { 176 return $this->limit; 177 } else { 178 return null; 179 } 180 } 181 182 /** 183 * 初始化当前页 184 * @return int 185 */ 186 private function setPage() { 187 if (!empty($_GET[$this->pageParam])) { 188 if ($_GET[$this->pageParam] > 0) { 189 if ($_GET[$this->pageParam] > $this->pageNum) 190 return $this->pageNum; 191 else 192 return $_GET[$this->pageParam]; 193 } 194 } 195 return 1; 196 } 197 198 /** 199 * 初始化url 200 * @param type $param 201 * @return string 202 */ 203 private function geturi($param) { 204 $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param; 205 $parse = parse_url($url); 206 if (isset($parse["query"])) { 207 parse_str($parse["query"], $params); 208 unset($params["page"]); 209 $url = $parse["path"] . "?" . http_build_query($params); 210 return $url; 211 } else { 212 return $url; 213 } 214 } 215 216 /** 217 * 本页开始条数 218 * @return int 219 */ 220 private function star() { 221 if ($this->total == 0) { 222 return 0; 223 } else { 224 return ($this->page - 1) * $this->pageSize + 1; 225 } 226 } 227 228 /** 229 * 本页结束条数 230 * @return type 231 */ 232 private function end() { 233 return min($this->page * $this->pageSize, $this->total); 234 } 235 236 /** 237 * 设置当前页大小 238 * @return type 239 */ 240 private function setPageSize() { 241 return $this->end() - $this->star() + 1; 242 } 243 244 /** 245 * 首页 246 * @return type 247 */ 248 private function frist() { 249 $html = ''; 250 if ($this->page == 1) { 251 $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true); 252 } else { 253 $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false); 254 } 255 return $html; 256 } 257 258 /** 259 * 上一页 260 * @return type 261 */ 262 private function prev() { 263 $html = ''; 264 if ($this->page > 1) { 265 $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false); 266 } else { 267 $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true); 268 } 269 return $html; 270 } 271 272 /** 273 * 分页数字列表 274 * @return type 275 */ 276 private function pagelist() { 277 $linkpage = ""; 278 $lastlist = floor($this->listnum / 2); 279 for ($i = $lastlist; $i >= 1; $i--) { 280 $page = $this->page - $i; 281 if ($page >= 1) { 282 $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); 283 } else { 284 continue; 285 } 286 } 287 $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true); 288 for ($i = 1; $i <= $lastlist; $i++) { 289 $page = $this->page + $i; 290 if ($page <= $this->pageNum) { 291 $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); 292 } else { 293 break; 294 } 295 } 296 return $linkpage; 297 } 298 299 /** 300 * 下一页 301 * @return type 302 */ 303 private function next() { 304 $html = ''; 305 if ($this->page < $this->pageNum) { 306 $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false); 307 } else { 308 $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true); 309 } 310 return $html; 311 } 312 313 /** 314 * 最后一页 315 * @return type 316 */ 317 private function last() { 318 $html = ''; 319 if ($this->page == $this->pageNum) { 320 $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true); 321 } else { 322 $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false); 323 } 324 return $html; 325 } 326 327 /** 328 * 跳转按钮 329 * @return string 330 */ 331 private function gopage() { 332 $html = ''; 333 $html.=' <input type="text" value="' . $this->page . '" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'" value="GO"/>'; 334 return $html; 335 } 336 337 /** 338 * 模板替换 339 * @param type $replace 替换内容 340 * @param type $result 条件 341 * @return type 342 */ 343 private function replace($url, $text, $result = true) { 344 $template = ($result ? $this->activeTemplate : $this->notActiveTemplate); 345 346 $html = str_replace('{url}', $url, $template); 347 $html = str_replace('{text}', $text, $html); 348 return $html; 349 } 350 }
第二款php分页类
1 <?php 2 /* 3 *本程序文件对分页程序进行了封装 4 * 5 */ 6 7 class Page_Link 8 { 9 var $page_max = 10; //一组页码的最大数 10 11 var $page_num = 10; //总页数 12 var $length = 20; //一页的数据条数 13 14 var $isNextPage = true; 15 var $isFirstPage = false; 16 17 function Calculation_Page_Num( $total ) 18 { 19 $this->page_num = ceil( $total / $this->length ); 20 return $this->page_num; 21 } 22 23 function Calculation_Min_Max( $act_page = 1 ) 24 { 25 // 定义左右偏移量 26 $py_left = 0; 27 $py_right = 0; 28 // 定义左右边界 29 $bj_left = 0; 30 $bj_right = 0; 31 // 定义滚动区间边界 32 $gd_left = 0; 33 $gd_right = 0; 34 // 判断是否需要分组 35 if ( ( $this->page_num - $this->page_max ) <= 0 ) 36 { 37 // 不需要分组 38 $bj_left = 1; 39 $bj_right = $this->page_num; 40 } 41 else 42 { 43 // 要进行分组 44 // 判断容量的奇偶 45 $tmp = $this->page_max % 2; 46 if ( $tmp === 1 ) 47 { 48 // 奇数 49 $py_left = $py_right = ( $this->page_max - 1 ) / 2; 50 } 51 else 52 { 53 // 偶数 54 $py_left = $this->page_max / 2 - 1; 55 $py_right = $this->page_max / 2; 56 } 57 // 计算滚动区间 58 $gd_left = 1 + $py_left; 59 $gd_right = $this->page_num - $py_right; 60 // 判断当前页是否落入了滚动区间 61 if ( $act_page >= $gd_left && $act_page <= $gd_right ) 62 { 63 // 区间内 64 $bj_left = $act_page - $py_left; 65 $bj_right = $act_page + $py_right; 66 } 67 else 68 { 69 // 区间外 70 if ( ( $act_page - $py_left ) <= 1 ) 71 { 72 // 左侧固定区间 73 $bj_left = 1; 74 $bj_right = $this->page_max; 75 } 76 else 77 { 78 $bj_left = $this->page_num - $this->page_max + 1; 79 $bj_right = $this->page_num; 80 } 81 } 82 } 83 84 $res = array(); 85 $res['min'] = $bj_left; 86 $res['max'] = $bj_right; 87 88 return $res; 89 90 } 91 // 主方法 92 function make_page( $total, $act_page, $url, $param ) 93 { 94 $page_num = $this->Calculation_Page_Num( $total ); 95 $arr_min_max = $this->Calculation_Min_Max( $act_page ); 96 97 if (!eregi("([?|&]$param=)", $url)) { 98 $url = strpos($url,"?")===false?$url."?":$url."&"; 99 $url = $url."$param=0"; 100 } 101 102 if ( $act_page > $page_num ) 103 { 104 $act_page = $page_num; 105 } 106 // 用正则把url改成正规的 107 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); 108 109 $res = array(); 110 $d = 0; 111 for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ ) 112 { 113 if ( $i == $act_page ) 114 { 115 $res[$d]['url'] = ''; 116 $res[$d]['name'] = $i; 117 $res[$d]['no'] = $i; 118 } 119 else 120 { 121 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 122 $res[$d]['name'] = $i; 123 $res[$d]['no'] = $i; 124 } 125 $d++; 126 } 127 128 if ( $this->isNextPage ) 129 { 130 $res = $this->make_before_next_link( $res, $act_page, $url, $param ); 131 } 132 if ( $this->isFirstPage ) 133 { 134 $res = $this->make_first_end_link( $res, $act_page, $url, $param ); 135 } 136 return $res; 137 } 138 //// 带总页数 139 function make_page_with_total( $total, $act_page, $url, $param ) 140 { 141 $page_num = $this->Calculation_Page_Num( $total ); 142 $arr_min_max = $this->Calculation_Min_Max( $act_page ); 143 144 if (!eregi("([?|&]$param=)", $url)) { 145 $url = strpos($url,"?")===false?$url."?":$url."&"; 146 $url = $url."$param=0"; 147 } 148 149 if ( $act_page > $page_num ) 150 { 151 $act_page = $page_num; 152 } 153 // 用正则把url改成正规的 154 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); 155 156 $res = array(); 157 $d = 0; 158 for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ ) 159 { 160 if ( $i == $act_page ) 161 { 162 $res[$d]['url'] = ''; 163 $res[$d]['name'] = $i; 164 $res[$d]['no'] = $i; 165 } 166 else 167 { 168 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 169 $res[$d]['name'] = $i; 170 $res[$d]['no'] = $i; 171 } 172 $d++; 173 } 174 175 if ( $this->isNextPage ) 176 { 177 $res = $this->make_before_next_link( $res, $act_page, $url, $param ); 178 } 179 if ( $this->isFirstPage ) 180 { 181 $res = $this->make_first_end_link( $res, $act_page, $url, $param ); 182 } 183 184 $total_num= ceil($total/$this->length); 185 $result['total']=$total_num; 186 $result['DATA']=$res; 187 return $result; 188 } 189 190 // 附加上一页和下一页 191 function make_before_next_link( $arr, $act, $url, $param ) 192 { 193 $tmp = array(); 194 195 $before = $act - 1; 196 $next = $act + 1; 197 198 if ( $before < 1 ) 199 { 200 $before = 1; 201 $tmp[0]['url'] = ''; 202 $tmp[0]['name'] = "上一页"; 203 $tmp[0]['no'] = $before; 204 } 205 else 206 { 207 $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url ); 208 $tmp[0]['name'] = "上一页"; 209 $tmp[0]['no'] = $before; 210 } 211 212 $counts = sizeof( $arr ); 213 $tmp_count = sizeof( $tmp ); 214 for( $i = 0;$i < $counts;$i++ ) 215 { 216 $tmp[$tmp_count]['url'] = $arr[$i]['url']; 217 $tmp[$tmp_count]['name'] = $arr[$i]['name']; 218 $tmp[$tmp_count]['no'] = $arr[$i]['no']; 219 $tmp_count++; 220 } 221 222 if ( $next > $this->page_num ) 223 { 224 $next = $this->page_num; 225 $tmp[$tmp_count]['url'] = ''; 226 $tmp[$tmp_count]['name'] = "下一页"; 227 $tmp[$tmp_count]['no'] = $next; 228 } 229 else 230 { 231 $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url ); 232 $tmp[$tmp_count]['name'] = "下一页"; 233 $tmp[$tmp_count]['no'] = $next; 234 } 235 236 return $tmp; 237 } 238 239 // 附加首页和尾页 240 function make_first_end_link( $arr, $act, $url, $param ) 241 { 242 $tmp = array(); 243 244 $before = 1; 245 $next = $this->page_num; 246 247 if ( $act == 1 ) 248 { 249 $before = 1; 250 $tmp[0]['url'] = ''; 251 $tmp[0]['name'] = "首页"; 252 $tmp[0]['no'] = $before; 253 } 254 else 255 { 256 $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url ); 257 $tmp[0]['name'] = "首页"; 258 $tmp[0]['no'] = $before; 259 } 260 261 $counts = sizeof( $arr ); 262 $tmp_count = sizeof( $tmp ); 263 for( $i = 0;$i < $counts;$i++ ) 264 { 265 $tmp[$tmp_count]['url'] = $arr[$i]['url']; 266 $tmp[$tmp_count]['name'] = $arr[$i]['name']; 267 $tmp[$tmp_count]['no'] = $arr[$i]['no']; 268 $tmp_count++; 269 } 270 271 if ( $act == $this->page_num ) 272 { 273 $tmp[$tmp_count]['url'] = ''; 274 $tmp[$tmp_count]['name'] = "尾页"; 275 $tmp[$tmp_count]['no'] = $next; 276 } 277 else 278 { 279 $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url ); 280 $tmp[$tmp_count]['name'] = "尾页"; 281 $tmp[$tmp_count]['no'] = $next; 282 } 283 284 return $tmp; 285 } 286 287 288 /** 289 * 带上一页<,下一页>,省略号的分页 290 * @param int $total 记录总条数 291 * @param int $act_page 当前页码 292 * @param string $url url 293 * @param int $maxpageicon 最大显示页码数 294 * @param int $style 上一页,下一页显示样式 295 * @param string $param url参数 296 */ 297 function make_page_with_points( $total,$act_page,$url,$maxpageicon,$style,$param ) 298 { 299 $page_num = $this->Calculation_Page_Num( $total ); //总页数 300 $arr_min_max = $this->Calculation_Min_Max( $act_page ); //最大页,最小页 301 if($total==0) 302 { 303 return ""; 304 305 } 306 if( $act_page > $page_num ) 307 { 308 $act_page = $page_num+1; 309 $page_num = $page_num+1; 310 } 311 312 switch ($style){ 313 case 1: 314 $name_before = '前一页'; 315 $name_next = '后一页'; 316 break; 317 case 2: 318 $name_before = '<'; 319 $name_next = '>'; 320 break; 321 case 3: 322 $name_before = '<<'; 323 $name_next = '>>'; 324 break; 325 default: 326 $name_before = '上一页'; 327 $name_next = '下一页'; 328 } 329 330 if (!eregi("([?|&]$param=)", $url)) { 331 $url = strpos($url,"?")===false?$url."?":$url."&"; 332 $url = $url."$param=0"; 333 } 334 335 // 用正则把url改成正规的 336 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); 337 $res = array(); 338 $no_before = $act_page-1; 339 $no_next = $act_page+1; 340 341 //总页数如果小于等于初始化最大呈现页数 342 if ($page_num<= ($maxpageicon + 1)) 343 { 344 //如果当前页数是首页 上一页无效 345 if ($act_page == 1) 346 { 347 $res[0]['url'] = ''; 348 $res[0]['name'] = $name_before; 349 $res[0]['no'] = $no_before; 350 } 351 else //上一页有效 352 { 353 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); 354 $res[0]['name'] = $name_before; 355 $res[0]['no'] = $no_before; 356 } 357 //循环添加页码 358 $d = 1; 359 for ($i = 1; $i <= $page_num; $i++) 360 { 361 if ($i != $act_page) 362 { 363 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 364 $res[$d]['name'] = $i; 365 $res[$d]['no'] = $i; 366 } 367 else //当前页,页码 368 { 369 $res[$d]['url'] = ''; 370 $res[$d]['name'] = $i; 371 $res[$d]['no'] = $i; 372 $res[$d]['attr'] = 'current'; 373 } 374 $d++; 375 } 376 $last_d = count($res); 377 //判断尾页 378 if($act_page == $page_num) //下一页无效 379 { 380 $res[$last_d]['url'] = ''; 381 $res[$last_d]['name'] = $name_next; 382 $res[$last_d]['no'] = $no_next; 383 } 384 else 385 { 386 $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url ); 387 $res[$last_d]['name'] = $name_next; 388 $res[$last_d]['no'] = $no_next; 389 } 390 }else if ($page_num > ($maxpageicon + 1))//如果总页数满足添加省略号 391 { 392 if ($act_page <= $maxpageicon) //如果当前页小于等于初始化数目 393 { 394 //如果当前页数是首页 上一页无效 395 if ($act_page == 1) 396 { 397 $res[0]['url'] = ''; 398 $res[0]['name'] = $name_before; 399 $res[0]['no'] = $no_before; 400 } 401 else //上一页有效 402 { 403 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); 404 $res[0]['name'] = $name_before; 405 $res[0]['no'] = $no_before; 406 } 407 //循环添加页码 408 $d = 1; 409 for ($i = 1; $i <= $maxpageicon; $i++) 410 { 411 if ($i != $act_page) 412 { 413 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 414 $res[$d]['name'] = $i; 415 $res[$d]['no'] = $i; 416 } 417 else //当前页,页码 418 { 419 $res[$d]['url'] = ''; 420 $res[$d]['name'] = $i; 421 $res[$d]['no'] = $i; 422 $res[$d]['attr'] = 'current'; 423 } 424 $d++; 425 } 426 $last_d = count($res); 427 //添加省略号 428 $res[$last_d]['url'] = ''; 429 $res[$last_d]['name'] = '...'; 430 $res[$last_d]['no'] = ''; 431 //总页数 432 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url ); 433 $res[$last_d+1]['name'] = $page_num; 434 $res[$last_d+1]['no'] = $page_num; 435 //下一页 436 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url ); 437 $res[$last_d+1]['name'] = $name_next; 438 $res[$last_d+1]['no'] = $no_next; 439 }else//如果当前页大于最大显示页面 440 { 441 if ($act_page > ($page_num - $maxpageicon))//满足后几页 442 { 443 //上一页 444 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); 445 $res[0]['name'] = $name_before; 446 $res[0]['no'] = $no_before; 447 //第一页 448 $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url ); 449 $res[1]['name'] = 1; 450 $res[1]['no'] = 1; 451 //省略号 452 $res[2]['url'] = ''; 453 $res[2]['name'] = '...'; 454 $res[2]['no'] = ''; 455 $d = 3; 456 for ($i = ($page_num - $maxpageicon + 1); $i <= $page_num; $i++) 457 { 458 if ($i != $act_page) 459 { 460 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 461 $res[$d]['name'] = $i; 462 $res[$d]['no'] = $i; 463 } 464 else //当前页,页码 465 { 466 $res[$d]['url'] = ''; 467 $res[$d]['name'] = $i; 468 $res[$d]['no'] = $i; 469 $res[$d]['attr'] = 'current'; 470 } 471 $d++; 472 } 473 $last_d = count($res); 474 //判断尾页 475 if($act_page == $page_num) //下一页无效 476 { 477 $res[$last_d]['url'] = ''; 478 $res[$last_d]['name'] = $name_next; 479 $res[$last_d]['no'] = $no_next; 480 } 481 else 482 { 483 $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url ); 484 $res[$last_d]['name'] = $name_next; 485 $res[$last_d]['no'] = $no_next; 486 } 487 488 }else//满足处在中间 489 { 490 //上一页 491 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); 492 $res[0]['name'] = $name_before; 493 $res[0]['no'] = $no_before; 494 //第一页 495 $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url ); 496 $res[1]['name'] = 1; 497 $res[1]['no'] = 1; 498 //省略号 499 $res[2]['url'] = ''; 500 $res[2]['name'] = '...'; 501 $res[2]['no'] = ''; 502 for ($i = ($act_page - ($maxpageicon - 2) / 2); $i <= floor($act_page+($maxpageicon - 2) / 2); $i++) 503 { 504 $i = ceil($i); 505 if ($i != $act_page) 506 { 507 $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); 508 $res[$d]['name'] = $i; 509 $res[$d]['no'] = $i; 510 } 511 else //当前页,页码 512 { 513 $res[$d]['url'] = ''; 514 $res[$d]['name'] = $i; 515 $res[$d]['no'] = $i; 516 $res[$d]['attr'] = 'current'; 517 } 518 $d++; 519 } 520 $last_d = count($res); 521 //加省略号 522 $res[$last_d]['url'] = ''; 523 $res[$last_d]['name'] = '...'; 524 $res[$last_d]['no'] = ''; 525 //当前页 526 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url ); 527 $res[$last_d+1]['name'] = $page_num; 528 $res[$last_d+1]['no'] = $page_num; 529 //下一页 530 $res[$last_d+2]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url ); 531 $res[$last_d+2]['name'] = $name_next; 532 $res[$last_d+2]['no'] = $no_next; 533 //exit; 534 } 535 } 536 } 537 return $res; 538 } 539 } 540 541 ?>