rendor();
echo '';
var_dump($p);
echo'
';
/**
* 分頁(yè)類
*/
class Page
{
protected $url; //URL
protected $pageCount; //總頁(yè)數(shù)
protected $total; //總條數(shù)
protected $num; //每頁(yè)顯示數(shù)
protected $page; //當(dāng)前頁(yè)
//初始化成員屬性
public function __construct($total,$num = 5)
{
//總條數(shù)
$this->total = ($total > 0 )? (int) $total : 1;
//每頁(yè)顯示條數(shù)
$this->num = $num;
//總頁(yè)數(shù)
$this->pageCount = $this->getPageCount();
//當(dāng)前頁(yè)
$this->page = $this->getCurrentPage();
//URL
$this->url = $this->getUrl();
}
//一次性返回所有的分頁(yè)信息
public function rendor()
{
return[
'first' => $this->first(),
'next' => $this->next(),
'prev' => $this->prev(),
'end' => $this->end(),
];
}
//limit方法,在未來(lái)分頁(yè)數(shù)據(jù)查詢的時(shí)候,直接返回對(duì)應(yīng)的limit0,5 這樣的字符串
public function limit()
{
$offset = ($this->page - 1) * $this->num;
$str = $offset.','.$this->num;
return $str;
}
//首頁(yè),設(shè)置page = 1
protected function first()
{
return $this->setQueryString('page=1');
}
//上一頁(yè)
protected function prev()
{
$page = ($this->page <= 1) ? 1 : ($this->page - 1);
return $this->setQueryString('page='.$page);
}
//下一頁(yè)
protected function next()
{
$page = ($this->page >= $this->pageCount) ? $this->pageCount : ($this->page + 1);
return $this->setQueryString('page='.$page);
}
//首頁(yè)
protected function end()
{
return $this->setQueryString('page='.$this->pageCount);
}
//一種情況有? 一種情況沒(méi)有?
protected function setQueryString($page)
{
//查找url中是否有問(wèn)號(hào)
if (stripos($this->url, '?')) {
return $this->url.'&'.$page;
} else {
//沒(méi)有問(wèn)號(hào)就拼接
return $this->url.'?'.$page;
}
}
//處理URL
protected function getUrl()
{
//獲取用戶的uri
$path = $_SERVER['REQUEST_URI'];
//解析uri
$par = parse_url($path);
//判斷用戶是否設(shè)置過(guò)query
if (isset($par['query'])) {
parse_str($par['query'],$query);
//檢查query 里面時(shí)候有page,如果有的話就干掉page
unset($query['page']);
$path = $par['path'] . '?'.http_build_query($query);
}
$path = rtrim($path,'?');
//協(xié)議:主機(jī):端口:文件和請(qǐng)求
//判斷是否定義過(guò)端口,并且端口是否為443,如果為443則是https協(xié)議,否則就是http協(xié)議
$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;
}
//處理當(dāng)前頁(yè)P(yáng)age
protected function getCurrentPage()
{
//如果有page,就返回轉(zhuǎn)換為int的page
if (isset($_GET['page'])) {
//得到頁(yè)碼
$page = (int) $_GET['page'];
//當(dāng)前頁(yè)碼不能給大于總頁(yè)碼
if ($page > $this->pageCount) {
$page = $this->pageCount;
}
if ($page < 1) {
$page = 1;
}
}else {
$page = 1;
}
return $page;
}
//處理總頁(yè)數(shù)
protected function getPageCount()
{
//進(jìn)一法取整
return ceil($this->total / $this->num);
}
}
分享名稱:PHP中面向?qū)ο蟮姆猪?yè)類
文章URL:
http://weahome.cn/article/ipscci.html