pdo php分页6,php pdo自动分页类代码与例子

/**

* 类名: PdoPage

* 作者:谢声涛 shishengsoft@gmail.com

* 描述: 继承自PDO类,增加了自动分页功能,类似MS ADO组件的自动分页功能。

*/ bbs.it-home.org

//-------------开始---------------

class PdoPage extends PDO {

public $RecordCount = 0; // 记录集的记录总数

public $AutoPage = false;// 启用自动分页功能

public $PageSize = 0;// 每页的记录行数

public $CurrentPage = 0; // 当前页

public $Pages = 0;// 总页数

public $BOF = false; // 游标到记录集之前

public $EOF = false; // 游标到记录集之后

private $RecordSet = null; // 记录集

private $mCurrentRow = -1; // 记录集中当前游标位置

private $Rows = 0;//总记录数

// 关闭连接

public function Close(){unset($this);}

// 分页查询

public function QueryEx($SqlString){

// 是否启用自动分页功能

if($this->AutoPage){

// 检查PageSize参数

if ($this->PageSize <=0) die("警告:PageSize不能为负数或零。");

// 计算总记录数

$rs = @parent::query($this->rebuildSqlString($SqlString));

$this->Rows = $rs->fetchColumn();

// 计算总页数

if ($this->Rows < $this->PageSize) {$this->Pages = 1;}

elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this->PageSize)+1;}

else {$this->Pages = intval($this->Rows/$this->PageSize);}

// 约束CurrentPage值,使之位于1到Pages之间。

if($this->CurrentPage < 1) {$this->CurrentPage =1;}

if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}

//计算偏移量

$Offset = $this->PageSize * ($this->CurrentPage - 1);

// 重组SQL语句,SqlString有分号则去掉

$SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";

}

// 查询并返回记录集

$rs = new PDOStatement();

$rs = @parent::query($SqlString);

$this->RecordSet = $rs->fetchAll();//returns an array.

$this->RecordCount = count($this->RecordSet);

if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}

return $this->RecordCount;

}

// 取得字段值

public function FieldValue($FieldName=""){

return ($this->RecordSet[$this->mCurrentRow][$FieldName]);

}

//--------移动记录集游标---------------

public function Move($RowPos){

if ($RowPos<0) $RowPos = 0;

if ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;

$this->mCurrentRow = $RowPos;

$this->EOF = false;

$this->BOF = false;

}

public function MoveNext(){

if($this->mCurrentRow < $this->RecordCount-1){

$this->mCurrentRow++;

$this->EOF = false;

$this->BOF = false;

}

else{

$this->EOF = true;

}

}

public function MovePrev(){

if($this->mCurrentRow > 0){

$this->mCurrentRow--;

$this->EOF = false;

$this->BOF = false;

}else{

$this->BOF = true;

}

}

public function MoveFirst(){

$this->mCurrentRow = 0;

$this->EOF = false;

$this->BOF = false;

}

public function MoveLast(){

$this->mCurrentRow = $this->RecordCount-1;

$this->EOF = false;

$this->BOF = false;

}

//--------------------------------------------------

// 用于执行插入、修改、删除等操作

public function Execute($SqlString){

return @parent::query($SqlString);

}

//-----------------私有函数-----------------------------

// 重新构造SQL语句,如将"select * from tb2"改写为"select count(*) from tb2",旨在提高查询效率。

private function rebuildSqlString($SqlString){

if(preg_match("/select[ ,./w+/*]+ from/",$SqlString,$marr)){

$columns = preg_replace("/select|from/","",$marr[0]);

$columns = preg_replace("//*/","/*",$columns);

$result = preg_replace("/$columns/"," count(*) ",$SqlString);

return $result;

}

}

//-------------结束-----------------------------------

}

//-------------结束-----------------------------------

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值