最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

<?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->current<1){
        $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-1<1){
        $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)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫(kù)操作入門教程》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

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

相關(guān)文章

最新評(píng)論

江油市| 昆明市| 甘德县| 房山区| 即墨市| 桃源县| 厦门市| 都昌县| 勃利县| 辽阳县| 邯郸县| 新昌县| 广南县| 濮阳县| 朝阳区| 台中县| 克山县| 玛纳斯县| 汕头市| 乡宁县| 安丘市| 东乌| 九江县| 海丰县| 阳春市| 应城市| 皮山县| 呼图壁县| 乡宁县| 湘西| 昔阳县| 佳木斯市| 兰溪市| 普兰店市| 特克斯县| 抚宁县| 洛扎县| 宁远县| 冕宁县| 龙山县| 车险|