PHP分页类
class Paging
{
//記錄的總條數
protected $total;
//每一頁的條數
protected $pageSize;
//當前頁
protected $page;
//頁碼的參數名字
protected $pageName;
//總頁數
protected $pageCount;
//基本URL
protected $url;
/**
- [__construct description]
- @param [type] $total [總頁數]
- @param integer $pageSize [每一頁的條數]
- @param string $pageName [頁碼參數的名字]
*/
public function __construct($total, $pageSize=5, $pageName='page')
{
$this->total = $total;
$this->pageSize = $pageSize;
$this->pageCount = ceil($total / $pageSize);
$this->pageName = $pageName;
$this->url = $this->getUrl();
$this->page = $this->getPage();
}
public function headPage()
{
return $this->setUrl(1);
}
public function prevPage()
{
if ($this->page < 2) {
$page = 1;
} else {
$page = $this->page - 1;
}
return $this->setUrl($page);
}
public function nextPage()
{
if ($this->page < $this->pageCount) {
$page = $this->page + 1;
} else {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function tailPage()
{
return $this->setUrl($this->pageCount);
}
public function givenPage($page)
{
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $this->setUrl($page);
}
public function listed()
{
return [
'head'
=>
$this->headPage(),
'prev'
=>
$this->prevPage(),
'next'
=>
$this->nextPage(),
'tail'
=>
$this->tailPage()];
}
protected function getPage()
{
//我們約定,確定頁碼的參數默認是page
if (empty($_GET[$this->pageName])) {
return 1;
}
$page = (int)$_GET[$this->pageName];
if ($page < 1) {
$page = 1;
} else if ($page > $this->pageCount) {
$page = $this->pageCount;
}
return $page;
}
protected function getUrl()
{
//獲取協議
$url = $_SERVER['REQUEST_SCHEME'] . '://';
//拼接主機
$url .= $_SERVER['HTTP_HOST'];
//拼接端口
$url .= ':' . $_SERVER['SERVER_PORT'];
//拼接URI
$requestUri = $_SERVER['REQUEST_URI'];
if (isset($_GET[$this->pageName])) {
$replaceStr = $this->pageName . '=' . $_GET[$this->pageName];
$replaceArr = [
$replaceStr.'&',
'&'.$replaceStr,
'?'.$replaceStr];
$requestUri = str_replace($replaceArr, '', $requestUri);
}
return $url . $requestUri;
}
protected function setUrl($page)
{
if (strpos($this->url, '?')) {
return $this->url . '&' . $this->pageName . '=' . $page;
} else {
return $this->url . '?' . $this->pageName . '=' . $page;
}
}
}
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。互聯網+時代,時刻要保持學習,攜手千鋒PHP,Dream It Possible。
轉載于:https://www.cnblogs.com/lhbryant/p/6929807.html
總結
- 上一篇: 一般试管婴儿的费用
- 下一篇: enote笔记语言(2)(ver0.3)