Home >Backend Development >PHP Tutorial >php pdo automatic paging code and examples

php pdo automatic paging code and examples

WBOY
WBOYOriginal
2016-07-25 08:51:481475browse
  1. /**
  2. * Class name: PdoPage
  3. * Author: Xie Shengtao shishengsoft@gmail.com
  4. * Description: Inherited from the PDO class, it adds automatic paging function, similar to the automatic paging function of MS ADO components.
  5. */ bbs.it-home.org
  6. //-------------Start-------- -------
  7. class PdoPage extends PDO {
  8. public $RecordCount = 0; // Total number of records in the recordset
  9. public $AutoPage = false;// Enable automatic paging
  10. public $PageSize = 0;// Each Number of record rows in the page
  11. public $CurrentPage = 0; // Current page
  12. public $Pages = 0; // Total number of pages
  13. public $BOF = false; // Before the cursor reaches the record set
  14. public $EOF = false; / / After the cursor reaches the record set
  15. private $RecordSet = null; // Record set
  16. private $mCurrentRow = -1; // Current cursor position in the record set
  17. private $Rows = 0; //Total number of records
  18. // Close the connection
  19. public function Close(){unset($this);}
  20. //Paging query
  21. public function QueryEx($SqlString){
  22. //Whether the automatic paging function is enabled
  23. if($this->AutoPage){
  24. //Check PageSize parameter
  25. if ($this->PageSize <=0) die("Warning: PageSize cannot be negative or zero.");
  26. // Calculate the total number of records
  27. $rs = @parent::query($this ->rebuildSqlString($SqlString));
  28. $this->Rows = $rs->fetchColumn();
  29. // Calculate the total number of pages
  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. // Constrain the CurrentPage value so that it is between 1 and Pages between.
  34. if($this->CurrentPage < 1) {$this->CurrentPage =1;}
  35. if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}
  36. //Calculate the offset
  37. $Offset = $this->PageSize * ($this->CurrentPage - 1);
  38. //Reorganize the SQL statement, remove the semicolon in SqlString
  39. $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";
  40. }
  41. // Query and return the record set
  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. // Get the field value
  50. public function FieldValue($FieldName=""){
  51. return ($this->RecordSet[$this->mCurrentRow][$FieldName]);
  52. }
  53. //---- ----Move record set cursor---------------
  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. // Used to perform insertion, modification, deletion, etc. Operation
  92. public function Execute($SqlString){
  93. return @parent::query($SqlString);
  94. }
  95. //-----------------Private function---- --------------------------
  96. // Reconstruct the SQL statement, such as rewriting "select * from tb2" to "select count(*) from tb2", designed to improve query efficiency.
  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. //----------------End---------------- ------------------
  106. }
  107. //-------------End------------ -----------------------
  108. ?>
Copy code

2. Usage examples: It is necessary to modify the MySQL user name, password, database name and other information.

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

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn