日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2025/6/15 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;

}

}

//-------------結束-----------------------------------

}

//-------------結束-----------------------------------

?>

總結

以上是生活随笔為你收集整理的pdo php分页6,php pdo自动分页类代码与例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。