主頁(yè) > 知識(shí)庫(kù) > PHP基于面向?qū)ο蠓庋b的分頁(yè)類(lèi)示例

PHP基于面向?qū)ο蠓庋b的分頁(yè)類(lèi)示例

熱門(mén)標(biāo)簽:電子圍欄 Mysql連接數(shù)設(shè)置 服務(wù)器配置 阿里云 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器

本文實(shí)例講述了PHP基于面向?qū)ο蠓庋b的分頁(yè)類(lèi)。分享給大家供大家參考,具體如下:

?php
  class Page
  {
    protected $num;//每頁(yè)顯示條數(shù)
    protected $total;//總記錄數(shù)
    protected $pageCount;//總頁(yè)數(shù)
    protected $current;//當(dāng)前頁(yè)碼
    protected $offset;//偏移量
    protected $limit;//分頁(yè)頁(yè)碼
    /**
     * 構(gòu)造方法
     * @param int $total 總記錄數(shù)
     * @param int $num  每頁(yè)顯示條數(shù)
     */
    public function __construct($total,$num=5)
    {
      //1.每頁(yè)顯示條數(shù)
      $this->num = $num;
      //2.總記錄數(shù)
      $this->total = $total;
      //3.總頁(yè)數(shù)
      $this->pageCount = ceil($total/$num);
      //4.偏移量
      $this->offset = ($this->current-1)*$num;
      //5.分頁(yè)頁(yè)碼
      $this->limit = "{$this->offset},{$this->num}";
      //6.初始化當(dāng)前頁(yè)
      $this->current();
    }
    /**
     * 初始化當(dāng)前頁(yè)
     */
    public function current(){
      $this->current = isset($_GET['page'])?$_GET['page']:'1';
      //判斷當(dāng)前頁(yè)最大范圍
      if ($this->current>$this->pageCount){
        $this->current = $this->pageCount;
      }
      //判斷當(dāng)前頁(yè)最小范圍
      if ($this->current1){
        $this->current = 1;
      }
    }
    /**
     * 訪問(wèn)沒(méi)權(quán)限訪問(wèn)的屬性
     * @param string $key 想訪問(wèn)的屬性
     * @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;
      }
    }
    /**
     * 處理分頁(yè)按鈕
     * @return string 拼接好的分頁(yè)按鈕
     */
    public function show(){
      //判斷初始頁(yè)碼
      $_GET['page'] = isset($_GET['page'])?$_GET['page']:'1';
      //將$_GET值賦給上下變量
      $first = $end = $prev = $next = $_GET;
      // var_dump($prev);
      //上一頁(yè)
      //判斷上一頁(yè)范圍
      if ($this->current-11){
        $prev['page'] = 1;
      }else{
        $prev['page'] = $this->current-1;
      }
      //下一頁(yè)
      //判斷下一頁(yè)范圍
      if ($this->current+1>$this->pageCount) {
        $next["page"] = $this->pageCount;
      }else{
        $next['page'] = $this->current+1;
      }
      /*
      首頁(yè)
      $first['page'] = 1; 
      //尾頁(yè)
      $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}頁(yè) ";
      $str .= "a href='{$url}?page=1'>首頁(yè)/a> ";
      $str .= "a href='{$prevpath}'>上一頁(yè)/a> ";
      $str .= "a href='{$nextpath}'>下一頁(yè)/a> ";
      $str .= "a href='{$url}?page={$this->pageCount}'>尾頁(yè)/a> ";
      return $str;
    }
  }
  //自行調(diào)試
  $a = new Page(10);
  echo $a->show();
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • php-fpm中max_children的配置
  • 使用Zookeeper分布式部署PHP應(yīng)用程序
  • php根據(jù)命令行參數(shù)生成配置文件詳解
  • 詳解PHP的抽象類(lèi)和抽象方法以及接口總結(jié)
  • Centos7.4環(huán)境安裝lamp-php7.0教程
  • 淺談PHP無(wú)限極分類(lèi)原理
  • 詳解PHP隊(duì)列的實(shí)現(xiàn)
  • PHP精確到毫秒秒殺倒計(jì)時(shí)實(shí)例詳解
  • PHP的簡(jiǎn)單跳轉(zhuǎn)提示的實(shí)現(xiàn)詳解
  • PHP-FPM的配置與優(yōu)化講解

標(biāo)簽:廣元 衢州 大理 衡水 萍鄉(xiāng) 江蘇 蚌埠 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP基于面向?qū)ο蠓庋b的分頁(yè)類(lèi)示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266