这个分页类,参考了慕课网和燕十八的分页类。
慕课网分页类学习网址:http://www.imooc.com/learn/419
效果图:
<?php
class page{
public $cpage; //传入的页码
public $totalnum; //总的记录数,数据
public $total_page; //总页数
public $per_page = 10; //每页的记录数
public $showpage = 5; //页码条显示的页码个数
public $pageoffset; //页码条偏移的页码个数
public $pageup; //上一页
public $pagedown; //下一页
public $start=1; //开始页码
public $end; //结束页码
public $fycod = array();
//初始化各个参数
public function __construct($cpage,$totalnum){
$this->cpage = $cpage;
$this->totalnum = $totalnum;
$this->pageoffset = ($this->showpage-1)/2;
$this->total_page = $this->totalnum/10;
$this->end = $this->total_page;
//总页数
$this->total_page = ceil($this->totalnum/$this->per_page);
}
//返回总的页数
public function gettotal_page(){
return $this->total_page;
}
//返回每页的记录数
public function getper_page(){
return $this->per_page;
}
//返回当前页
public function getcpage()
{
return $this->cpage;
}
//修改当前页
public function modcpage($cpage){
$this->cpage = $cpage;
}
//返回上一页
public function getpageup(){
if($this->cpage<=1){
return $this->pageup = 1;
}
return $this->pageup = $this->cpage -1;
}
//返回下一页
public function getpagedown(){
if($this->cpage < $this->total_page){
return $this->pagedown = $this->cpage+1;
}
}
//返回一个数组,关于页码的各个参数
public function getpages(){
$pages = array();
$pages[‘total_page‘] = $this->total_page;
$pages[‘per_page‘] = $this->per_page;
$pages[‘cpage‘] = $this->cpage;
$pages[‘pageup‘] = $this->getpageup();
$pages[‘pagedown‘] = $this->getpagedown();
$pages[‘start‘] = $this->start;
$pages[‘end‘] = $this->end;
$pages[‘pageoffset‘] = $this->pageoffset;
$pages[‘showpage‘] = $this->showpage;
return $pages;
}
//分页代码函数,返回一个数组,作用:分页的页码数
public function getfycod(){
if($this->cpage > $this->pageoffset){
$this->start = $this->cpage - $this->pageoffset;
$this->end = $this->total_page > $this->cpage + $this->pageoffset ? $this->cpage + $this->pageoffset : $this->total_page;
}else{
$this->start = 1;
$this->end = $this->total_page > $this->showpage ? $this->showpage : $this->total_page;
}
if($this->cpage + $this->pageoffset > $this->total_page){
$this->start = $this->start - ($this->cpage + $this->pageoffset - $this->end);
}
for($i=$this->start;$i<=$this->end;$i++){
$this->fycod[] = $i;
}
return $this->fycod;
}
}