<?php
$page = new Page(5, 60);
var_dump($page->allUrl());
class Page {
//每一页显示多少条数据
protected $countOfPerPage;
//一共有多少条数据
protected $totalCount;
//当前页
protected $currPage;
//总页数
protected $totalPage;
//url
protected $url;
public function __construct ($countOfPerPage, $totalCount) {
$this->countOfPerPage = $countOfPerPage;
$this->totalCount = $totalCount;
$this->totalPage = $this->getTotalPage();
$this->page = $this->getPage();
$this->url = $this->getUrl();
echo $this->url;
}
protected function getUrl () {
$scheme = $_SERVER['REQUEST_SCHEME'];
$host = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$uri = $_SERVER['REQUEST_URI'];
$uriArray = parse_url($uri);
$path = $uriArray['path'];
if (!empty($uriArray['query'])) {
parse_str($uriArray['query'], $array);
unset($array['page']);
$query = http_build_query($array);
if ('' != $query) {
$path = $path . '?' . $query;
}
}
return $scheme . '://' . $host . ':' . $port . $path;
}
protected function getPage () {
if (empty($_GET['page'])) {
$page = 1;
} else if ($_GET['page'] > $this->totalPage) {
$page = $this->totalPage;
} else if ($_GET['page'] < 1) {
$page = 1;
} else {
$page = $_GET['page'];
}
return $page;
}
protected function getTotalPage () {
return ceil($this->totalCount / $this->countOfPerPage);
}
public function allUrl () {
return [
'first' => $this->first(),
'prev' => $this->prev(),
'next' => $this->next(),
'last' => $this->last()
];
}
public function first () {
return $this->setUrl('page=1');
}
protected function setUrl ($str) {
if (strstr($this->url, '?')) {
$url = $this->url . '&' . $str;
} else {
$url = $this->url . '?' . $str;
}
return $url;
}
public function next () {
if ($this->currPage + 1 > $this->totalPage) {
$page = $this->totalPage;
} else {
$page = $this->currPage + 1;
}
return $this->setUrl('page=' . $page);
}
public function prev () {
if ($this->currPage-1 < 1) {
$page = 1;
} else {
$page = $this->currPage - 1;
}
return $this->setUrl('page=' . $page);
}
public function last () {
return $this->setUrl('page=' . $this->totalPage);
}
public function limit () {
$offset = ($this->page - 1) * $this->countOfPerPage;
return $offset . ',' . $this->countOfPerPage;
}
}
php高级实战-分页类
最新推荐文章于 2024-11-03 20:36:30 发布