pdo php分页6,php pdo自动分页类代码与例子
/**
* 類名: PdoPage
* 作者:謝聲濤 shishengsoft@gmail.com
* 描述: 繼承自PDO類,增加了自動(dòng)分頁(yè)功能,類似MS ADO組件的自動(dòng)分頁(yè)功能。
*/ bbs.it-home.org
//-------------開始---------------
class PdoPage extends PDO {
public $RecordCount = 0; // 記錄集的記錄總數(shù)
public $AutoPage = false;// 啟用自動(dòng)分頁(yè)功能
public $PageSize = 0;// 每頁(yè)的記錄行數(shù)
public $CurrentPage = 0; // 當(dāng)前頁(yè)
public $Pages = 0;// 總頁(yè)數(shù)
public $BOF = false; // 游標(biāo)到記錄集之前
public $EOF = false; // 游標(biāo)到記錄集之后
private $RecordSet = null; // 記錄集
private $mCurrentRow = -1; // 記錄集中當(dāng)前游標(biāo)位置
private $Rows = 0;//總記錄數(shù)
// 關(guān)閉連接
public function Close(){unset($this);}
// 分頁(yè)查詢
public function QueryEx($SqlString){
// 是否啟用自動(dòng)分頁(yè)功能
if($this->AutoPage){
// 檢查PageSize參數(shù)
if ($this->PageSize <=0) die("警告:PageSize不能為負(fù)數(shù)或零。");
// 計(jì)算總記錄數(shù)
$rs = @parent::query($this->rebuildSqlString($SqlString));
$this->Rows = $rs->fetchColumn();
// 計(jì)算總頁(yè)數(shù)
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;}
//計(jì)算偏移量
$Offset = $this->PageSize * ($this->CurrentPage - 1);
// 重組SQL語句,SqlString有分號(hào)則去掉
$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]);
}
//--------移動(dòng)記錄集游標(biāo)---------------
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;
}
//--------------------------------------------------
// 用于執(zhí)行插入、修改、刪除等操作
public function Execute($SqlString){
return @parent::query($SqlString);
}
//-----------------私有函數(shù)-----------------------------
// 重新構(gòu)造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;
}
}
//-------------結(jié)束-----------------------------------
}
//-------------結(jié)束-----------------------------------
?>
總結(jié)
以上是生活随笔為你收集整理的pdo php分页6,php pdo自动分页类代码与例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 对象存储过程,从php中的存储过
- 下一篇: matlab中矩阵的累加,累加和与累乘积