欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP基于面向?qū)ο蠓庋b的分頁類示例

 更新時間:2019年03月15日 08:37:08   作者:小菜鳥有大夢想  
這篇文章主要介紹了PHP基于面向?qū)ο蠓庋b的分頁類,結(jié)合實例形式分析了php分頁類針對頁碼判斷、顯示等操作的封裝及分頁類使用相關(guān)操作技巧,需要的朋友可以參考下

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

<?php
  class Page
  {
    protected $num;//每頁顯示條數(shù)
    protected $total;//總記錄數(shù)
    protected $pageCount;//總頁數(shù)
    protected $current;//當前頁碼
    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.初始化當前頁
      $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;
      }
    }
    /**
     * 訪問沒權(quán)限訪問的屬性
     * @param string $key 想訪問的屬性
     * @return float|int|string 返回對應(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è)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

最新評論