首頁  >  文章  >  後端開發  >  php pdo自動分頁類別程式碼與例子

php pdo自動分頁類別程式碼與例子

WBOY
WBOY原創
2016-07-25 08:51:481448瀏覽
  1. /**
  2. * 類別名稱: PdoPage
  3. * 作者:謝聲濤 shishengsoft@gmail.com
  4. * 說明: 繼承自PDO類,增加了自動分頁功能,類似MS ADO元件的自動分頁功能。
  5. */ bbs.it-home.org
  6. //--------- ----開始---------------
  7. class PdoPage extends PDO {
  8. public $RecordCount = 0; // 記錄集的總記錄數
  9. public $AutoPage = false;// 啟用自動分頁功能
  10. public $PageSize = 0;// 每頁的記錄行數
  11. public $CurrentPage = 0; // 目前頁
  12. public $Pages = 0;//總頁數
  13. public $BOF = false; // 遊標到記錄集之前
  14. public $EOF = false; // 遊標到記錄集之後
  15. private $RecordSet = null; // 記錄集
  16. private $mCurrentRow = -1; // 記錄集中目前遊標位置
  17. private $Rows = 0;//總記錄數
  18. // 關閉連線
  19. public function Close(){unset($this) ;}
  20. // 分頁查詢
  21. public function QueryEx($SqlString){
  22. // 是否啟用自動分頁功能
  23. if($this->AutoPage){
  24. // 檢查PageSize參數
  25. if ($this->PageSize // 計算總記錄數
  26. $rs = @parent::query( $this->rebuildSqlString($SqlString));
  27. $this->Rows = $rs->fetchColumn();
  28. // 計算總頁數
  29. if ($this->Rows PageSize) {$this->Pages = 1;}
  30. elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this-> PageSize) 1;}
  31. else {$this->Pages = intval($this->Rows/$this->PageSize);}
  32. // 約束CurrentPage值,使其位於1到Pages之間。
  33. if($this->CurrentPage CurrentPage =1;}
  34. if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this- >Pages;}
  35. //計算偏移
  36. $Offset = $this->PageSize * ($this->CurrentPage - 1);
  37. // 重組SQL語句,SqlString有分號則去掉
  38. $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";
  39. }
  40. // 查詢並傳回記錄集
  41. $$ rs = new PDOStatement();
  42. $rs = @parent::query($SqlString);
  43. $this->RecordSet = $rs->fetchAll();//returns an array.
  44. $ this->RecordCount = count($this->RecordSet);
  45. if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}
  46. return $this->RecordCount;
  47. }
  48. // 取得欄位值
  49. public function FieldValue($FieldName=""){
  50. return ($this->RecordSet[$this->mCurrentRow] [$FieldName]);
  51. }
  52. //--------移動記錄集遊標---------------
  53. public function Move($ RowPos){
  54. if ($RowPosif ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;
  55. $this ->mCurrentRow = $RowPos;
  56. $this->EOF = false;
  57. $this->BOF = false;
  58. }
  59. public function MoveNext(){
  60. if($this-this-this- >mCurrentRow RecordCount-1){
  61. $this->mCurrentRow ;
  62. $this->EOF = false;
  63. $this->BOF = false;
  64. }
  65. else{
  66. $this->EOF = true;
  67. }
  68. }
  69. public function MovePrev(){
  70. if($this->mCurrentRow > 0){
  71. $this- >mCurrentRow--;
  72. $this->EOF = false;
  73. $this->BOF = false;
  74. }else{
  75. $this->BOF = true;
  76. }
  77. }
  78. public function MoveFirst(){
  79. $this->mCurrentRow = 0;
  80. $this->EOF = false;
  81. $this->BOF = false;
  82. }
  83. }
  84. $this->BOF = false;
  85. }
  86. }
  87. public function MoveLast(){
  88. $this->mCurrentRow = $this->RecordCount-1;
  89. $this->EOF = false;
  90. $this->BOF = false;
  91. }}
  92. }}
  93. //--------------------------------------------- -----
  94. // 用於執行插入、修改、刪除等操作
  95. public function Execute($SqlString){
  96. return @parent::query($SqlString);
  97. }
  98. //-----------------私有函數--------------------------- --
  99. // 重新建構SQL語句,如將"select * from tb2"改寫為"select count(*) from tb2",旨在提高查詢效率。
  100. private function rebuildSqlString($SqlString){
  101. if(preg_match("/select[ ,./w /*] from/",$SqlString,$marr)){
  102. $columns = preg_replace(" /select|from/","",$marr[0]);
  103. $columns = preg_replace("//*/","/*",$columns);
  104. $result = preg_replace(" /$columns/"," count(*) ",$SqlString);
  105. return $result;
  106. }
}
//-------------結束----------------------------------- } //----- --------結束------------------------------------------------
?> 複製程式碼

2、使用範例: 需修改MySQL使用者名稱、密碼、資料庫名稱等資訊。

  1. include_once("./pdopage_class.php");
  2. $db = new PdoPage(" mysql:host=localhost;dbname=mydb","root","123456");
  3. $db->Execute("set character set gbk;");
  4. $db->AutoPage = false;
  5. $db->PageSize = 6;
  6. $db->CurrentPage = 1;
  7. $db->QueryEx("select * from tb2;");
  8. $db->MoveFirst();
  9. while (!$db->EOF) {
  10. echo $db->FieldValue("id"),"/t",$db->FieldValue("name"),"/t",$ db->FieldValue("age"),"/n";
  11. $db->MoveNext();
  12. }
  13. $db->Close();
  14. ?>

複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn