<?php
include("mysql/config.inc.php");
class pageClass{
private $pageIndex;//当前页码
private $pageSize;//每页显示记录数
private $count;//总记录数
private $pageCount;//总页数
private $condition;
private $order;//排序
private $mysql;//选择的数据库操作类
private $tableName;//需要分页的表名
//当前页数属性
function setPageIndex($value){
$this->pageIndex=$value;
}
function getPageIndex(){
return $this->pageIndex;
}
//每页显示记录数属性
function setPageSize($value){
$this->pageSize=$value;
//改变每页显示的记录数会影响总页数
$this->getPageCount();
}
function getPageSize(){
return $this->pageSize;
}
//总记录数属性
function getCount(){
$this->InnerGetCount();//调用自己的内部函数
return $this->count;
}
//总页数属性
function getPageCount(){//总页数等于总记录数除以每页显示记录数
$this->InnerGetCount();//调用自己的内部函数
return $this->pageCount=ceil(($this->count/$this->pageSize));
if($this->pageCount==0){
$this->pageCount=1;
return $this->pageCount;
}
}
//查询条件
function setCondition($value){
$this->condition=$value;
//设置条件会影响总记录数和总页数
$this->getPageCount();
}
//排序
function setOrder($value){
$this->order=$value;
}
//选择数据库操作类
function setMysql(){
$this->mysql;
}
//构造函数初始化配置
function __construct($tableName){
$this->tableName=$tableName;
$this->pageIndex=1;
$this->pageSize=1;
$this->pageCount=1;
$this->count=0;
$this->condition='';
$this->order='';
//数据库操作类对象
if(DBTYPE=='mysql'){
include_once("mysql/mysqlhelper.php");
$this->mysql=new mysqlhelper(DB);
}
elseif(DBTYPE=='mysqli'){
include_once("mysql/MysqliHelper.php");
$this->mysql=new mysqlihelper(DB);
}
}
//获取分页记录
function getRecord(){
$sql="select * from $this->tableName where 1=1 $this->condition $this->order limit ".($this->pageIndex-1)*$this->pageSize.",".$this->pageSize."";
return $this->mysql->Execute($sql); //一定要有return 返回值
}
//获取总记录数
private function innerGetCount(){
$sql="select count(*) from $this->tableName where 1=1 $this->condition";
$result=$this->mysql->Execute($sql,2);
$this->count=$result[0][0];
}
}
?>
【PHP高级】分页类
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.