PHP基于面向對象封裝的分頁類示例
更新時間:2019年03月15日 08:37:08 作者:小菜鳥有大夢想
這篇文章主要介紹了PHP基于面向對象封裝的分頁類,結合實例形式分析了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 .= "<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;
}
}
//自行調試
$a = new Page(10);
echo $a->show();
?>
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+mysql數據庫操作入門教程》、《php+mysqli數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP網絡編程技巧總結》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
apache2.2.4+mysql5.0.77+php5.2.8安裝精簡
linux下apache php環(huán)境的配置方法。2009-04-04
PHP使用finfo_file()函數檢測上傳圖片類型的實現方法
這篇文章主要介紹了PHP使用finfo_file()函數檢測上傳圖片類型的實現方法,結合實例形式分析了finfo_file()函數的功能、使用方法及相關注意事項,需要的朋友可以參考下2017-04-04
PHP中spl_autoload_register()和__autoload()區(qū)別分析
這篇文章主要介紹了spl_autoload_register()和__autoload()區(qū)別,需要的朋友可以參考下2014-05-05
php class中public,private,protected的區(qū)別以及實例分析
本篇文章是對php class中public,private,protected的區(qū)別以及實例進行了詳細的分析介紹,需要的朋友參考下2013-06-06

