0514-86177077
9:00-17:00(工作日)
本文實例講述了PHP基于面向?qū)ο蠓庋b的分頁類。分享給大家供大家參考,具體如下:
?php class Page { protected $num;//每頁顯示條數(shù) protected $total;//總記錄數(shù) protected $pageCount;//總頁數(shù) protected $current;//當前頁碼 protected $offset;//偏移量 protected $limit;//分頁頁碼 /** * 構(gòu)造方法 * @param int $total 總記錄數(shù) * @param int $num 每頁顯示條數(shù) */ public function __construct($total,$num=5) { //1.每頁顯示條數(shù) $this->num = $num; //2.總記錄數(shù) $this->total = $total; //3.總頁數(shù) $this->pageCount = ceil($total/$num); //4.偏移量 $this->offset = ($this->current-1)*$num; //5.分頁頁碼 $this->limit = "{$this->offset},{$this->num}"; //6.初始化當前頁 $this->current(); } /** * 初始化當前頁 */ public function current(){ $this->current = isset($_GET['page'])?$_GET['page']:'1'; //判斷當前頁最大范圍 if ($this->current>$this->pageCount){ $this->current = $this->pageCount; } //判斷當前頁最小范圍 if ($this->current1){ $this->current = 1; } } /** * 訪問沒權(quán)限訪問的屬性 * @param string $key 想訪問的屬性 * @return float|int|string 返回對應要改變的條件 */ public function __get($key){ if ($key == "limit") { return $this->limit; } if ($key == "offset") { return $this->offset; } if ($key == "current") { return $this->current; } } /** * 處理分頁按鈕 * @return string 拼接好的分頁按鈕 */ public function show(){ //判斷初始頁碼 $_GET['page'] = isset($_GET['page'])?$_GET['page']:'1'; //將$_GET值賦給上下變量 $first = $end = $prev = $next = $_GET; // var_dump($prev); //上一頁 //判斷上一頁范圍 if ($this->current-11){ $prev['page'] = 1; }else{ $prev['page'] = $this->current-1; } //下一頁 //判斷下一頁范圍 if ($this->current+1>$this->pageCount) { $next["page"] = $this->pageCount; }else{ $next['page'] = $this->current+1; } /* 首頁 $first['page'] = 1; //尾頁 $end['page'] = $this->pageCount; */ //拼接路徑 $url = "http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]; //拼接數(shù)組url地址欄后綴?傳入?yún)?shù) //http://xxx/xxx/Page.class.php?page=值 $prev = http_build_query($prev); $next = http_build_query($next); // $first = http_build_query($first); // $end = http_build_query($end); //拼接完整路徑 $prevpath = $url."?".$prev; $nextpath = $url."?".$next; // $firstpath = $url."?".$first; // $endpath = $url."?".$end; $str = "共有{$this->total}條記錄 共有{$this->pageCount}頁 "; $str .= "a href='{$url}?page=1'>首頁/a> "; $str .= "a href='{$prevpath}'>上一頁/a> "; $str .= "a href='{$nextpath}'>下一頁/a> "; $str .= "a href='{$url}?page={$this->pageCount}'>尾頁/a> "; return $str; } } //自行調(diào)試 $a = new Page(10); echo $a->show(); ?>
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
標簽:荊門 遵義 寧夏 徐州 遂寧 武威 常州 吐魯番
上一篇:淺談PHP無限極分類原理
下一篇:詳解PHP的抽象類和抽象方法以及接口總結(jié)
Copyright ? 1999-2012 誠信 合法 規(guī)范的巨人網(wǎng)絡通訊始建于2005年
蘇ICP備15040257號-8