PHP基于面向?qū)ο蠓庋b的分頁類示例
本文實(shí)例講述了PHP基于面向?qū)ο蠓庋b的分頁類。分享給大家供大家參考,具體如下:
<?php
class Page
{
protected $num;//每頁顯示條數(shù)
protected $total;//總記錄數(shù)
protected $pageCount;//總頁數(shù)
protected $current;//當(dāng)前頁碼
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.初始化當(dāng)前頁
$this->current();
}
/**
* 初始化當(dāng)前頁
*/
public function current(){
$this->current = isset($_GET['page'])?$_GET['page']:'1';
//判斷當(dāng)前頁最大范圍
if ($this->current>$this->pageCount){
$this->current = $this->pageCount;
}
//判斷當(dāng)前頁最小范圍
if ($this->current<1){
$this->current = 1;
}
}
/**
* 訪問沒權(quán)限訪問的屬性
* @param string $key 想訪問的屬性
* @return float|int|string 返回對(duì)應(yīng)要改變的條件
*/
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-1<1){
$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();
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中使用數(shù)組實(shí)現(xiàn)堆棧數(shù)據(jù)結(jié)構(gòu)的代碼
堆棧是一種數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)形式,是廣泛用來存取數(shù)據(jù)的一種容器2012-02-02
apache2.2.4+mysql5.0.77+php5.2.8安裝精簡
linux下apache php環(huán)境的配置方法。2009-04-04
php獲取ajax的headers方法與內(nèi)容實(shí)例
下面小編就為大家分享一篇php獲取ajax的headers方法與內(nèi)容實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
PHP使用finfo_file()函數(shù)檢測上傳圖片類型的實(shí)現(xiàn)方法
這篇文章主要介紹了PHP使用finfo_file()函數(shù)檢測上傳圖片類型的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了finfo_file()函數(shù)的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04
PHP中spl_autoload_register()和__autoload()區(qū)別分析
這篇文章主要介紹了spl_autoload_register()和__autoload()區(qū)別,需要的朋友可以參考下2014-05-05
php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫操作簡單示例
這篇文章主要介紹了php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了php + ajax 寫入數(shù)據(jù)庫基本原理、操作技巧與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2020-05-05
php class中public,private,protected的區(qū)別以及實(shí)例分析
本篇文章是對(duì)php class中public,private,protected的區(qū)別以及實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

