自己写的比较完善的分页类

// 
*/

class fenye {
//分页设置字段
private $offset;
//记录总数
private $total;
//每页显示的数量
private $preNum;
//总页数
private $pageNums;
//查询表
private $table;
//排序条件
private $stipulation;
//记录内容;
private $content;
//显示页码
private $pagination;
//显示下拉跳转菜单
private $downList;

//构造函数 初始化变量
function __construct($preNum,$table,$stipulation) {
$this->preNum = $preNum;
$this->table = $table;
$this->stipulation = $stipulation;
$this->analyseUrl();
}
//分析url,查找offset
function analyseUrl($url='') {
$url = $url?$url:$_SERVER['REQUEST_URI'];
if(strpos($url,"?")===false) {
$this->offset = 0;
} else {
$urls = explode("?",$url);
$urls = explode("=",$urls[1]);
$this->offset = intval($urls[1]);
//echo $this->offset;
//判断是否有恶意的offset设置 有则设置offset为空
$this->offset = $this->offset>=0&&$this->offset<$this->getPageNums()?$this->offset:0;
//echo $this->offset;
}
}
//获取当前页码
function getCurrentpageNum() {
return($this->offset+1);
}
//计算总记录数
function calculateTotal() {
$sql = "SELECT * FROM {$this->table} WHERE 1=1";
//echo $sql;
include_once('class_db.php');
$db = new db;
$query = $db->query($sql);
$this->total = $db->getNum($query);
}
//获取总记录数
function getTotal() {
$this->calculateTotal();
if($this->total>0) {
return $this->total;
} else {
return("没有相关记录");
}
}
//计算&获取总页码
function getPageNums() {
$this->pageNums = ceil($this->getTotal()/$this->preNum);
return $this->pageNums;
}
//根据offset获取相关数据 默认值 为显示五个页码使用.
function calculateContent($num='') {
$startNum = $num?$num:$this->offset;
$starNum = $startNum*$this->preNum;
$sql = "SELECT * FROM {$this->table} WHERE 1=1 ORDER BY {$this->stipulation} DESC limit ";
$sql .= $startNum;
$sql .= ',';
$sql .= $this->preNum;
//echo $sql."
";
include_once('class_db.php');
$db = new db;
$query = $db->query($sql);
while($result=$db->assoc($query)) {
$this->content[] = $result;
}
}
//获取当前显示的内容
function getContent($num='') {
$this->calculateContent($num);
return $this->content?$this->content:'没用相关记录';
}
//上一页
function previousPage() {
$previousPage = ($this->offset-1)?($this->offset-1):'';
return("?offset=".$previousPage);

}
//下一页
function nextPage() {
$nextPage = $this->offset+1<=$this->getPageNums()?$this->offset+1:'';
return("?offset=".$nextPage);
}
//获取页码
//此函数需在calpulateUrl函数之后使用 参数需用奇数
//显示的效果如: 23456
//例子说明: 总页数为 8
function getPagination($showNum=5) {
$frontPage = intval($showNum/2); //echo "frontPage".$frontPage."
";
//echo $frontPage;
$lastPage = $this->pageNums-$frontPage; //echo "lastPage".$lastPage."
";
//echo $lastPage;
$half = $frontPage;
//总页数与显示页码个数比较

//总页数大于等于要显示的页码个数
/*---------------------------------------------------------*/
if($this->pageNums>=$showNum) {
//处于中间状态
//有页码字 4,5
//与之对应的offset 3,4
/*-----------------------------------------*/
if($this->offset>$frontPage&&$this->offset<$lastPage) {
//$this->pagination = "test";
for($i=$half;$i>0;$i--) {
$this->pagination[] = array("href"=>"?offset=".($this->offset-$i),"pagination"=>($this->offset-$i+1));
}
$this->pagination[] = array("href"=>"?offset=".$this->offset,"pagination"=>$this->offset+1,"check"=>1);
for($j=1;$j<=$half;$j++) {
$this->pagination[] = array("href"=>"?offset=".($this->offset+$j),"pagination"=>($this->offset+$j+1));
}
}
//处于后面的那几个页码
//有页码字 6,7,8
//与之对应的offset 5,6,7
else if($this->offset>$frontPage&&$this->offset>=$lastPage) {
//$this->pagination = "test";
for($k=$showNum;$k>0;$k--) {
if($this->offset==($this->pageNums-$k)) {
$this->pagination[] = array("href"=>"?offset=".($this->pageNums-$k),"pagination"=>($this->pageNums-$k+1),"check"=>1);
} else {
$this->pagination[] = array("href"=>"?offset=".($this->pageNums-$k),"pagination"=>($this->pageNums-$k+1));
}
}
}
//处于前面的那几个页码
//有页码字 1,2,3
//与之对应的offset 0,1,2
else {
//$this->pagination = "test";
for($q=1;$q<=$showNum;$q++) {
//$this->pagination = "test";
if($this->offset==$q-1) {
$this->pagination[] = array("href"=>"?offset=".($q-1),"pagination"=>$q,"check"=>1);
} else {
$this->pagination[] = array("href"=>"?offset=".($q-1),"pagination"=>$q);
}
}
}
}
//总页数小于要显示的页码个数
//显示总页数个页码
else {
for($z=1;$z<=$this->pageNums;$z++) {
if($this->offset==$z-1) {
$this->pagination[] = array("href"=>"?offset=".($z-1),"pagination"=>$z,"check"=>1);
} else {
$this->pagination[] = array("href"=>"?offset=".($z-1),"pagination"=>$z);
}
}
}
return $this->pagination;
}
//显示下拉菜单式跳转菜单
//此操作需要js和select的onchange事件的支持,以下是示例,供参考
/*



*/
function getDownList() {
for($i=0;$i<$this->pageNums;$i++) {
if($this->offset==$i) {
$this->downList[] = array("value"=>"?offset=$i","item"=>$i+1,"selected"=>1);
} else {
$this->downList[] = array("value"=>"?offset=$i","item"=>$i+1);
}
}
return $this->downList;
}
}

/*

$fenye = new fenye(5,"liuyan","time");
print_r($fenye->getTotal());
echo "
";
print_r($fenye->previousPage());
echo "
";
print_r($fenye->getPageNums());
echo "
";
print_r($fenye->nextPage());
echo "
";
print_r($fenye->getPagination());
echo "
";
print_r($fenye->getDownList());
*/
?>

相关文章

发表评论

返回顶部