-
-
/**
- * 類別名稱: 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 // 計算總記錄數
- $rs = @parent::query( $this->rebuildSqlString($SqlString));
- $this->Rows = $rs->fetchColumn();
- // 計算總頁數
- if ($this->Rows 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 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 ($RowPosif ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;
- $this ->mCurrentRow = $RowPos;
- $this->EOF = false;
- $this->BOF = false;
- }
- public function MoveNext(){
- if($this-this-this- >mCurrentRow 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;
- }
- }
- $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;
- }
} //-------------結束----------------------------------- } //----- --------結束------------------------------------------------ ?> 複製程式碼2、使用範例:
需修改MySQL使用者名稱、密碼、資料庫名稱等資訊。
-
-
- include_once("./pdopage_class.php");
- $db = new PdoPage(" mysql:host=localhost;dbname=mydb","root","123456");
- $db->Execute("set character set gbk;");
- $db->AutoPage = false;
- $db->PageSize = 6;
- $db->CurrentPage = 1;
- $db->QueryEx("select * from tb2;");
- $db->MoveFirst();
- while (!$db->EOF) {
- echo $db->FieldValue("id"),"/t",$db->FieldValue("name"),"/t",$ db->FieldValue("age"),"/n";
- $db->MoveNext();
- }
- $db->Close();
?>
-
複製程式碼
|