Phppdo automatic paging code and example-php Tutorial

Source: Internet
Author: User
Phppdo automatic paging code and example

  1. /**
  2. * Class name: PdoPage
  3. * Author: Xie Shengtao shishengsoft@gmail.com
  4. * Description: it inherits from the PDO class and adds the automatic paging function, similar to the automatic paging function of the ms ado component.
  5. */Bbs.it-home.org
  6. // ------------- Start ---------------
  7. Class PdoPage extends PDO {
  8. Public $ RecordCount = 0; // total number of records in the record set
  9. Public $ AutoPage = false; // enable the automatic paging function
  10. Public $ PageSize = 0; // number of records per page
  11. Public $ CurrentPage = 0; // current page
  12. Public $ Pages = 0; // total number of Pages
  13. Public $ BOF = false; // cursor before record set
  14. Public $ EOF = false; // cursor after record set
  15. Private $ RecordSet = null; // record set
  16. Private $ mCurrentRow =-1; // the 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. // Query by page
  21. Public function QueryEx ($ SqlString ){
  22. // Whether to enable automatic paging
  23. If ($ this-> AutoPage ){
  24. // Check The 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.
  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. if SqlString has a semicolon, remove it.
  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. // Obtain the field value
  50. Public function FieldValue ($ FieldName = ""){
  51. Return ($ this-> RecordSet [$ this-> mCurrentRow] [$ FieldName]);
  52. }
  53. // -------- Move the 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 for insert, modify, delete, and other operations
  92. Public function Execute ($ SqlString ){
  93. Return @ parent: query ($ SqlString );
  94. }
  95. // ----------------- Private function -----------------------------
  96. // Re-construct an SQL statement. for example, rewrite "select * from tb2" to "select count (*) from tb2" 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. ?>

2. example: you need 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. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.