日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php面向对象分页,PHP基于面向对象封装的分页类示例

發布時間:2025/5/22 php 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基于面向对象封装的分页类示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。