|
将如下代码放到:application/ibraries/page.php 中即可。
PHP复制代码
<?php
/**
* 分页类
* @author 如水(www.rushui.net)
*/
final class Page {
private $page; //当前页
private $page_num; //总页数
private $offset; //数据库查询的偏移量(查询开始的记录)
private $prev_page; //当前的上一页码
private $next_page; //当前的下一页码
private $ctrl_num; //数字控制页码显示(不计算首页和末页在内,显示个数公式($ctrl_num*2+1))
private $max_num; //页码最大显示个数
private $go_num; //数字控制go中的显示
private $go_max; //go中的显示最大个数
public $total; //总记录数
public $page_size; //每页显示记录数
public $className; //样式名
public $prevText; //上一页按扭显示文本
public $nextText; //下一页按扭显示文本
public function setpage ($total=0,$page_size='',$page=null,$ctrl_num='',$go_num=''){
//默认分布配置
$page_size = $page_size == '' ? _config ('limit_default') : $page_size;
$ctrl_num = $ctrl_num == '' ? _config ('ctrl_num') : $ctrl_num;
$go_num = $go_num == '' ? _config ('go_num') : $go_num;
$this->total =$total;
$this->page_size =$page_size;
$this->ctrl_num =$ctrl_num;
$this->go_num =$go_num;
$this->page =$page;
$this->init();
}
private function init (){
//计算总页数
$this->page_num=ceil($this->total/$this->page_size);
//当前页码
if(empty($this->page) && isset($_GET['p'])) {
$this->page=intval($_GET["p"]);
}
$this->page=intval($this->page);
if($this->page > $this->page_num) {$this->page = $this->page_num;}
if($this->page<1) {$this->page=1;}
//数据库查询的偏移量 (当前页-1)*每页条数
$this->offset = ($this->page -1) * $this->page_size;
//计算上一页
$this->prev_page = ($this->page<=1) ? 1 : $this->page - 1;
//计算下一页
$this->next_page = ($this->page == $this->page_num) ? $this->page_num : $this->page + 1;
//当前页的开始记录显示文本
$this->start_page_txt = scope ($this->page_size*($this->page-1)+1,1,$this->total);
//当前页的结束记录显示文本
$this->stop_page_txt = scope ($this->page_size*$this->page,1,$this->total);
//最大页码显示数
$this->max_num = $this->ctrl_num*2+1;
$this->go_max = $this->go_num*2+1;
//开始页码&结束页码
if($this->max_num >= $this->page_num){
$this->start_page = 1;
$this->stop_page = $this->page_num;
}else{
if(($this->page_num-$this->page)<=$this->ctrl_num){
$this->start_page = $this->page_num-$this->max_num+1;
$this->stop_page = $this->page_num;
}else{
$this->start_page = $this->page-$this->ctrl_num;
$this->stop_page = $this->page+$this->ctrl_num;
}
if($this->page<=$this->ctrl_num){
$this->start_page = 1;
$this->stop_page = $this->max_num;
}
}
//设置go中的开始页码&结束页码
if($this->go_max >= $this->page_num){
$this->start_go = 1;
$this->stop_go = $this->page_num;
}else{
if(($this->page_num-$this->page)<=$this->go_num){
$this->start_go = $this->page_num-$this->go_max+1;
$this->stop_go = $this->page_num;
}else{
$this->start_go = $this->page-$this->go_num;
$this->stop_go = $this->page+$this->go_num;
}
if($this->page<=$this->go_num){
$this->start_go = 1;
$this->stop_go = $this->go_max;
}
}
}
//页码
public function getPageInfo (){
return array(
"total" =>$this->total, //总记录数
"page" =>$this->page, //当前页
"num" =>$this->page_size, //每页显示记录数
"page_num" =>$this->page_num, //总页数
"prev_page" =>$this->prev_page, //当前的上一页码
"next_page" =>$this->next_page, //当前的下一页码
"offset" =>$this->offset //数据库查询的偏移量(查询开始的记录,常用于limit操作)
);
}
//显示页码状态
public function getpage_txt (){
return "显示 <b>" . $this->total . "</b> 条中的 <b>" . $this->start_page_txt . "</b> 到 <b>" . $this->stop_page_txt . "</b> 条 第 <b>". $this->page . "/" . $this->page_num . "</b> 页";
}
//输出分页
public function getpage ($home='',$go='yes'){
$ac_prev = ($this->page==1)? ' class="prev disabled"':'';
$ac_next = ($this->page==$this->page_num)? ' class="next disabled"':'';
$page = '<li '.$ac_prev.'><a href="'.deal_url ('p',$this->prev_page).'">«上一页</a></li>';
for ($i=$this->start_page;$i<=$this->stop_page ; $i++) {
$ac = ($i==$this->page)? ' class="active"':'';
$page .= '<li '.$ac.'><a href="'.deal_url ('p',$i).'">'.$i.'</a></li>';
}
$page .= '<li'.$ac_next.'><a href="'.deal_url ('p',$this->next_page).'">下一页»</a></li>';
if($go=='yes'){
$page .= '<li>  Go:';
$page .= "<select name='' id='' style='width:80px' onchange='rsjs.seturl(\"".deal_url ('p','"+this.value').")'>";
$page .= '<option value="1" >Home</option>';
for($i=$this->start_go;$i<=$this->stop_go ; $i++){
$ac_selected = ($this->page==$i)? ' selected':'';
$page .= '<option value="'.$i.'" '.$ac_selected.'>'.$i.'</option>';
}
$page .= '<option value="'.$this->page_num.'" >Last</option>';
$page .= '</select>';
$page .= '</li>';
}
return $this->page_num>1 ? $page :'';
}
}
复制代码
|
|