php面向对象分页,PHP基于面向对象封装的分页类示例
本文實例講述了php基于面向對象封裝的分頁類。分享給大家供大家參考,具體如下:
class page
{
protected $num;//每頁顯示條數
protected $total;//總記錄數
protected $pagecount;//總頁數
protected $current;//當前頁碼
protected $offset;//偏移量
protected $limit;//分頁頁碼
/**
* 構造方法
* @param int $total 總記錄數
* @param int $num 每頁顯示條數
*/
public function __construct($total,$num=5)
{
//1.每頁顯示條數
$this->num = $num;
//2.總記錄數
$this->total = $total;
//3.總頁數
$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->current<1){
$this->current = 1;
}
}
/**
* 訪問沒權限訪問的屬性
* @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-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"];
//拼接數組url地址欄后綴?傳入參數
//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 .= "首頁 ";
$str .= "上一頁 ";
$str .= "下一頁 ";
$str .= "尾頁 ";
return $str;
}
}
//自行調試
$a = new page(10);
echo $a->show();
?>
希望本文所述對大家php程序設計有所幫助。
希望與廣大網友互動??
點此進行留言吧!
總結
以上是生活随笔為你收集整理的php面向对象分页,PHP基于面向对象封装的分页类示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: header+php+xiazai_ph
- 下一篇: php redis消息队列用哪种好,ph