執行個體詳解php分頁類

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了萬能的php分頁類,具有一定的參考價值,感興趣的小夥伴們可以參考一下

代碼如下:

<?php/*核心:首頁、上一頁、下一頁、尾頁的url*//*超全域$_SERVER*/$page = new Page(5,60);var_dump($page->allUrl());class Page{//   每頁顯示的個數  protected $number;//   一共有多少資料  protected $totalCount;//   當前頁  protected $page;//   url  protected $url;    public function __construct($number,$totalCount){    $this->number= $number;    $this->totalCount = $totalCount;    //得到總頁數    $this->totalPage = $this->getTotalPage();    //得到當前頁數    $this->page = $this->getPage();    //得到URL    $this->url = $this->getUrl();    echo $this->url;  }  /*得到總頁數並向上取整*/  protected function getTotalPage(){    return   ceil($this->totalCount/$this->number);  }  /**/  protected function getPage(){    if (empty($_GET['page'])){      $page=1;    }elseif ($_GET['page'] > $this->totalPage){      $page = $this->totalPage;    }elseif ($_GET["page"]<1){      $page = 1;    }else{      $page = $_GET['page'];    }    return $page;  }  protected function getUrl(){    //得到協議名    $scheme = $_SERVER['REQUEST_SCHEME'];    //得到主機名稱    $host= $_SERVER['SERVER_NAME'];    //得到連接埠號碼    $port = $_SERVER['SERVER_PORT'];    //得到路徑和請求字串    $url = $_SERVER['REQUEST_URI'];    /*中間做處理,要將page=5等這種字串拼接URL    中,所以如果原來的url中有page這個參數,我們首先    需要將原來的page參數給清空*/    $urlArray = parse_url($url);//     var_dump($urlArray);    $path = $urlArray['path'];    if (!empty($urlArray['query'])){      //將query中的值轉化為數組      parse_str($urlArray['query'],$array);      //如果他有page就將它刪掉      unset($array['page']);      //將關聯陣列轉化為query      $query = http_build_query($array);      //不為空白的話就與path連結      if ($query != ''){        $path = $path.'?'.$query;      }    }    return 'http://'. $host.':'.$port.$path;  }  protected function setUrl($str){    if (strstr($this->url, '?')){      $url = $this->url.'&'.$str;    }else{      $url = $this->url.'?'.$str;    }    return $url;  }  /*所有的url*/  public function allUrl(){    return [      'first' => $this->first(),      'next' => $this->next(),      'prev'=> $this->prev(),      'end'=> $this->end(),    ];  }  /*首頁*/  public function first(){    return $this->setUrl('page=1');  }  /*下一頁*/  public function next(){    //根據當前page得帶下一頁的頁碼    if ($this->page+1 > $this->totalPage) {      $page = $this->totalPage;    }else{      $page = $this->page+1;    }    return $this->setUrl('page='.$page);  }  /*上一頁*/  public function prev(){    //根據當前page得帶下一頁的頁碼    if ($this->page - 1 < 1) {      $page = 1;    }else{      $page = $this->page-1;    }    return $this->setUrl('page='.$page);  }  /*尾頁*/  public function end(){    return $this->setUrl('page='.$this->totalPage);  }  /*limit 0,5*/  public function limit(){    $offset = ($this->page-1)*$this->number;    return $offset.','.$this->number;  }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.