php pdo自動分頁類代碼與例子

來源:互聯網
上載者:User
  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 <=0) die("警告:PageSize不能為負數或零。");
  26. // 計算總記錄數
  27. $rs = @parent::query($this->rebuildSqlString($SqlString));
  28. $this->Rows = $rs->fetchColumn();
  29. // 計算總頁數
  30. if ($this->Rows < $this->PageSize) {$this->Pages = 1;}
  31. elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this->PageSize)+1;}
  32. else {$this->Pages = intval($this->Rows/$this->PageSize);}
  33. // 約束CurrentPage值,使之位於1到Pages之間。
  34. if($this->CurrentPage < 1) {$this->CurrentPage =1;}
  35. if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}
  36. //計算位移量
  37. $Offset = $this->PageSize * ($this->CurrentPage - 1);
  38. // 重組SQL語句,SqlString有分號則去掉
  39. $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";
  40. }
  41. // 查詢並返回記錄集
  42. $rs = new PDOStatement();
  43. $rs = @parent::query($SqlString);
  44. $this->RecordSet = $rs->fetchAll();//returns an array.
  45. $this->RecordCount = count($this->RecordSet);
  46. if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}
  47. return $this->RecordCount;
  48. }
  49. // 取得欄位值
  50. public function FieldValue($FieldName=""){
  51. return ($this->RecordSet[$this->mCurrentRow][$FieldName]);
  52. }
  53. //--------移動記錄集遊標---------------
  54. public function Move($RowPos){
  55. if ($RowPos<0) $RowPos = 0;
  56. if ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;
  57. $this->mCurrentRow = $RowPos;
  58. $this->EOF = false;
  59. $this->BOF = false;
  60. }
  61. public function MoveNext(){
  62. if($this->mCurrentRow < $this->RecordCount-1){
  63. $this->mCurrentRow++;
  64. $this->EOF = false;
  65. $this->BOF = false;
  66. }
  67. else{
  68. $this->EOF = true;
  69. }
  70. }
  71. public function MovePrev(){
  72. if($this->mCurrentRow > 0){
  73. $this->mCurrentRow--;
  74. $this->EOF = false;
  75. $this->BOF = false;
  76. }else{
  77. $this->BOF = true;
  78. }
  79. }
  80. public function MoveFirst(){
  81. $this->mCurrentRow = 0;
  82. $this->EOF = false;
  83. $this->BOF = false;
  84. }
  85. public function MoveLast(){
  86. $this->mCurrentRow = $this->RecordCount-1;
  87. $this->EOF = false;
  88. $this->BOF = false;
  89. }
  90. //--------------------------------------------------
  91. // 用於執行插入、修改、刪除等操作
  92. public function Execute($SqlString){
  93. return @parent::query($SqlString);
  94. }
  95. //-----------------私人函數-----------------------------
  96. // 重新構造SQL語句,如將"select * from tb2"改寫為"select count(*) from tb2",旨在提高查詢效率。
  97. private function rebuildSqlString($SqlString){
  98. if(preg_match("/select[ ,./w+/*]+ from/",$SqlString,$marr)){
  99. $columns = preg_replace("/select|from/","",$marr[0]);
  100. $columns = preg_replace("//*/","/*",$columns);
  101. $result = preg_replace("/$columns/"," count(*) ",$SqlString);
  102. return $result;
  103. }
  104. }
  105. //-------------結束-----------------------------------
  106. }
  107. //-------------結束-----------------------------------
  108. ?>
複製代碼

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. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.