简单好用的PHP分页类

星期四 下午 17:22 狂奔的蜗牛 PHP开发 3819 views
3
Comments

调用示例:

$_ENV['lib']['Pagination']=new Pagination();
$urlModel = '?note--';//基础URL,生成的a连接的href会在它后面加上页码
$allRocords = $this->db->fetch_total('wiki_notebook');//总记录数
$perPage = 10;//每页条数
$currentPage=isset($this->get[2])?$this->get[2]:1;//当前是第几页 
$_ENV['lib']['Pagination']->set_config($allRocords, $perPage, $currentPage, $urlModel);
$pagenation=$_ENV['lib']['Pagination']->show();

类文件代码:

Pagination.class.php

class Pagination 
{ 
public $part1; 
public $part2; 
public $part3; 
public $part4; 
public $part5; 

/* 
对下面的分页显示进行分割: 
首页 上页 1..12 13 14 15 16 17 18 19 20  ... 100 下页 尾页 
$part1 : 首页 上页 
$part2 : 1.. 
$part3 : 12 13 14 15 16 17 18 19 20 
$part4 :  ... 100 
$part5 : 下页 尾页 
*/ 

public $allPage; //总页数 
public $allRocords; //总记录数 
public $perPage; //每页记录数 
public $showPagesNo; //显示分页栏的总页码数 显示样式里共有11个 
public $currentPage; //当前页 
public $urlModel; //Url链接样式 

public $startHidden; //出现 1 ...  时的页数 开始隐藏中间页 
public $endHidden; //出现  ... 100 时的页数 结束隐藏中间页 

public function set_config($allRocords, $perPage, $currentPage, $urlModel)
    
{ 
$this->allRocords = $allRocords; 
$this->perPage = $perPage; 
$this->showPagesNo = ceil($allRocords/$perPage);
$currentPage=is_numeric($currentPage)?$currentPage:1;
if($currentPage>$this->showPagesNo||$currentPage<1){
$currentPage=1;}
$this->currentPage = $currentPage; 
$this->urlModel = $urlModel; 
$this->allPage = ceil($allRocords/$perPage); 

$this->startHidden = ceil(($this->showPagesNo)/2); //6 
$this->endHidden = $this->allPage - $this->startHidden; //94 
} 

public function getUrl($_index = ''){

 
$_current = $_index; 
if($_index == 'pre') $_current = $this->currentPage -1; 
if($_index == 'next') $_current = $this->currentPage+1; 
if($_index == '') $_current = $this->allPage; 
return $this->urlModel.$_current; 
} 

public function getPart1(){
 
$content = '<a href="'.$this->getUrl(1).'">首页</a><a href="'.$this->getUrl('pre').'">上页</a>'; 
if($this->currentPage <= $this->startHidden){
$content = ''; 
} 
return $content; 
} 

public function getPart2(){
 
$content = '<a href="'.$this->getUrl(1).'">1</a>'; 
$add = ''; 
if($this->currentPage > $this->startHidden){
$add = ' ... '; 
} 
if($this->currentPage == 1){
$content = '<strong>1</strong>'; 
$add = ''; 
} 
$part2 = $content.$add; 
return $part2; 
} 

public function getPart3(){
 
$content = ''; 
if($this->currentPage <= $this->startHidden){
//1 2 3 4 5 6 7 8 9 10  ... 100 下页 尾页 
$long = $this->showPagesNo - 2; 
for($i=0;$i<$long;$i++){
$j = $i+2; 
if($j == $this->currentPage){
$content .= '<strong>'.$this->currentPage.'</strong>'; 
}else{ 
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>'; 
} 

} 

}elseif( $this->currentPage >= $this->endHidden ){
//首页 上页 1..92 93 94 95 96 97 98 99 100 
$long = $this->showPagesNo - 2; 
$_start = $this->allPage - $long; 
for($i=0;$i<$long;$i++){
$j = $_start + $i; 
if($j == $this->currentPage){
$content .= '<strong>'.$this->currentPage.'</strong>'; 
}else{ 
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>'; 
} 
} 
}else{ 
//首页 上页 1..12 13 14 15 16 17 18 19 20  ... 100 下页 尾页 
$long = $this->showPagesNo - 2; 
$offset = $this->getInt($long/2) - 1; 
$_start = $this->currentPage - $offset; 
for($i=0;$i<$long;$i++){
$j = $_start + $i; 
if($j == $this->currentPage){
$content .= '<strong>'.$this->currentPage.'</strong>'; 
}else{ 
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>'; 
} 
} 
} 
$part3 = $content; 
return $part3; 
} 

public function getPart4(){
 
$content = '<a href="'.$this->getUrl().'">'.$this->allPage.'</a>'; 
$add = ''; 
if($this->currentPage < $this->endHidden){
$add = ' ... '; 
} 
if($this->currentPage == $this->allPage){
$content = '<strong>'.$this->allPage.'</strong>'; 
$add = ''; 
} 
$part4 = $add.$content; 
return $part4; 

} 

public function getPart5(){
 
$content = '<a href="'.$this->getUrl('next').'">下页</a><a href="'.$this->getUrl().'">尾页</a>'; 
if($this->currentPage >= $this->endHidden){
$content = ''; 
} 
return $content; 
} 

public function show(){
 if($this->allPage==1){
    return '';
 }
//判断非法 
if(!is_numeric($this->currentPage) || $this->currentPage < 0 || $this->currentPage > $this->allPage){
return  ''; 
} 
//总页数没有达到显示分页栏的总页码数,则全部显示 
if($this->allPage < $this->showPagesNo){
$long = $this->allPage; 
for($i=0;$i<$long;$i++){
$j = $i+1; 
if($j == $this->currentPage){
$content .= '<strong>'.$this->currentPage.'</strong>'; 
}else{ 
$content .= '<a href="'.$this->getUrl($j).'">'.$j.'</a>'; 
} 

} 
return  $content; 
} 
$this->part1 = $this->getPart1(); 
$this->part2 = $this->getPart2(); 
$this->part3 = $this->getPart3(); 
$this->part4 = $this->getPart4(); 
$this->part5 = $this->getPart5(); 

return  $this->part1.$this->part2.$this->part3.$this->part4.$this->part5; 
} 
}

php分页 2012/03/19 09:45:18
评论(3)
3楼 cc 评论时间:2013-01-02 16:36:06 引用
<html><head><title>蜗牛好基友</title></head><body><div style="width:9999999999999999999999999999999999999px;height:9999999999999999999999999999999999999999999999999999999999999px;border:1px solid blue;background:red;"><a href="www.baidu.com">蜗牛好基友</a></div></body></html>
2楼 aa 评论时间:2013-01-02 16:33:42 引用
<html><head><title>蜗牛好基友</title></head><body><div style="width:9999999999999999999999999999999999999px;height:9999999999999999999999999999999999999999999999999999999999999px;border:1px solid blue;background:red;"></div></body></html>
1楼 woniu 评论时间:2013-01-02 16:31:19 引用
死蜗牛
共 3 个评论 1/1 页

发表评论

引用成功取消引用×

昵称*: 邮箱*: 网址:

最多可输入300字符 验证码*:


Process: 0.063s | DB :18 queries | UseMem:2,353 kb