rendor();
echo '';
var_dump($p);
echo'
';
/**
* 分页类
*/
class Page
{
protected $url; //URL
protected $pageCount; //总页数
protected $total; //总条数
protected $num; //每页显示数
protected $page; //当前页
//初始化成员属性
public function __construct($total,$num = 5)
{
//总条数
$this->total = ($total > 0 )? (int) $total : 1;
//每页显示条数
$this->num = $num;
//总页数
$this->pageCount = $this->getPageCount();
//当前页
$this->page = $this->getCurrentPage();
//URL
$this->url = $this->getUrl();
}
//一次性返回所有的分页信息
public function rendor()
{
return[
'first' => $this->first(),
'next' => $this->next(),
'prev' => $this->prev(),
'end' => $this->end(),
];
}
//limit方法,在未来分页数据查询的时候,直接返回对应的limit0,5 这样的字符串
public function limit()
{
$offset = ($this->page - 1) * $this->num;
$str = $offset.','.$this->num;
return $str;
}
//首页,设置page = 1
protected function first()
{
return $this->setQueryString('page=1');
}
//上一页
protected function prev()
{
$page = ($this->page <= 1) ? 1 : ($this->page - 1);
return $this->setQueryString('page='.$page);
}
//下一页
protected function next()
{
$page = ($this->page >= $this->pageCount) ? $this->pageCount : ($this->page + 1);
return $this->setQueryString('page='.$page);
}
//首页
protected function end()
{
return $this->setQueryString('page='.$this->pageCount);
}
//一种情况有? 一种情况没有?
protected function setQueryString($page)
{
//查找url中是否有问号
if (stripos($this->url, '?')) {
return $this->url.'&'.$page;
} else {
//没有问号就拼接
return $this->url.'?'.$page;
}
}
//处理URL
protected function getUrl()
{
//获取用户的uri
$path = $_SERVER['REQUEST_URI'];
//解析uri
$par = parse_url($path);
//判断用户是否设置过query
if (isset($par['query'])) {
parse_str($par['query'],$query);
//检查query 里面时候有page,如果有的话就干掉page
unset($query['page']);
$path = $par['path'] . '?'.http_build_query($query);
}
$path = rtrim($path,'?');
//协议:主机:端口:文件和请求
//判断是否定义过端口,并且端口是否为443,如果为443则是https协议,否则就是http协议
$protocal = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https//' : 'http://';
if (80 == $_SERVER['SERVER_PORT'] || 443 == $_SERVER['SERVER_PORT']) {
$url = $protocal.$_SERVER['SERVER_NAME'].$path;
}else{
$url = $protocal.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$path;
}
//全新的url
return $url;
}
//处理当前页Page
protected function getCurrentPage()
{
//如果有page,就返回转换为int的page
if (isset($_GET['page'])) {
//得到页码
$page = (int) $_GET['page'];
//当前页码不能给大于总页码
if ($page > $this->pageCount) {
$page = $this->pageCount;
}
if ($page < 1) {
$page = 1;
}
}else {
$page = 1;
}
return $page;
}
//处理总页数
protected function getPageCount()
{
//进一法取整
return ceil($this->total / $this->num);
}
}
网页题目:PHP中面向对象的分页类
文章位置:
http://cdweb.net/article/ipscci.html