分页类
<?php
class Page {
private $table;//查询的表
private $thisPage;//当前页
private $arr_re;//返回结果数组
private $limit;//每页显示的行数
private $count;//表总的记录数
private $reStr;//返回的分页字符串
private $baseName;//
function __construct($table){
$this->table=$table;
$sql='select count(*) from '.$table;
$res=mysql_query($sql);
$arr=mysql_fetch_row($res);
mysql_free_result($res);//销毁结果集
$this->count=$arr[0];//计算出表中总的行数
$this->baseName=basename(__FILE__);
}
/**
* @thisPage int //开始从第几天记录开始显示,一般是从url获取的,所以要验证是否为整数
* @limit int//每页显示的记录数
*/
public function getArray ($thisPage,$limit) {//返回查询二维数组
$pat="/^[1-9]+[0-9]*$/";//验证开始位置是否为正整数
if(!preg_match($pat,$thisPage)){
$thisPage=1;
}
$thisPage=(($thisPage*$limit)>$this->count)?ceil($this->count/$limit):$thisPage;
$this->thisPage=$thisPage;
$this->limit=$limit;
$sql='select * from '.$this->table.' limit '.(($thisPage-1)*$limit).','.$limit;
$result=mysql_query($sql);
while($arr=mysql_fetch_assoc($result)){
$this->arr_re[]=$arr;
}
mysql_free_result($result);//销毁结果集
return $this->arr_re;
}
/**
*@type int //返回分页字符串的类型,暂时只支持一种类型
*
*/
public function showPage ($type=1) {//返回分页
$type='page_'.$type.'()';
return $this->page_1();
}
private function page_1() {
$pageNum=ceil($this->count/$this->limit);//总的页数
if($this->thisPage==1){//第一页
$this->reStr.='<span>首页</span><span>上一页</span>';
}else{
$this->reStr.='<a href="'.$this->baseName.'?page=1">首页</a><a href="'.$this->baseName.'?page='.($this->thisPage-1).'">上一页< /a>';
}
if($this->thisPage>5){
$this->reStr.=' <a href="'.$this->baseName.'?page='.(floor($this->thisPage/5-1)*5).'">...</a>';
}
$tmp=$this->thisPage%5?$this->thisPage%5:5;
for($i=$tmp-1;$i>0;$i--){//当前页前面的页数
$this->reStr.=' <a href="'.$this->baseName.'?page='.($this->thisPage-$i).'">'.($this->thisPage-$i).'</a>';
}
$this->reStr.='<strong>'.$this->thisPage.'</strong>';//当前页
for($i=1;$i<=5-$tmp;$i++){//当前页后面的页数
$this->reStr.=' <a href="'.$this->baseName.'?page='.($this->thisPage+$i).'">'.($this->thisPage+$i).'</a>';
}
if(($this->count/$this->limit-$this->thisPage)>=4){
$this->reStr.=' <a href="'.$this->baseName.'?page='.(ceil($this->thisPage/5)*5+1).'">...</a>';
}
if($this->thisPage==$pageNum){//最后一页
$this->reStr.='<span>下一页</span><span> 尾页</span>';
}else{
$this->reStr.=' <a href="'.$this->baseName.'?page='.($this->thisPage+1).'">下一页< /a> <a href="'.$this->baseName.'?page='.$pageNum.'">尾页</a>';
}
return $this->reStr;
}
}
?>