- /**
- * Class name: PdoPage
- * Author: Xie Shengtao shishengsoft@gmail.com
- * Description: it inherits from the PDO class and adds the automatic paging function, similar to the automatic paging function of the ms ado component.
- */Bbs.it-home.org
- // ------------- Start ---------------
- Class PdoPage extends PDO {
- Public $ RecordCount = 0; // total number of records in the record set
- Public $ AutoPage = false; // enable the automatic paging function
- Public $ PageSize = 0; // number of records per page
- Public $ CurrentPage = 0; // current page
- Public $ Pages = 0; // total number of Pages
- Public $ BOF = false; // cursor before record set
- Public $ EOF = false; // cursor after record set
- Private $ RecordSet = null; // record set
- Private $ mCurrentRow =-1; // the current cursor position in the record set
- Private $ Rows = 0; // total number of records
- // Close the connection
- Public function Close () {unset ($ this );}
- // Query by page
- Public function QueryEx ($ SqlString ){
- // Whether to enable automatic paging
- If ($ this-> AutoPage ){
- // Check The PageSize parameter
- If ($ this-> PageSize <= 0) die ("Warning: PageSize cannot be negative or zero. ");
- // Calculate the total number of records
- $ Rs = @ parent: query ($ this-> rebuildSqlString ($ SqlString ));
- $ This-> Rows = $ rs-> fetchColumn ();
- // Calculate the total number of pages
- If ($ this-> Rows <$ this-> 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 );}
- // Constrain the CurrentPage value so that it is between 1 and Pages.
- If ($ this-> CurrentPage <1) {$ this-> CurrentPage = 1 ;}
- If ($ this-> CurrentPage> $ this-> Pages) {$ this-> CurrentPage = $ this-> Pages ;}
- // Calculate the offset
- $ Offset = $ this-> PageSize * ($ this-> CurrentPage-1 );
- // Reorganize the SQL statement. if SqlString has a semicolon, remove it.
- $ SqlString = str_replace (";", "", $ SqlString). "LIMIT $ Offset, $ this-> PageSize ;";
- }
- // Query and return the record set
- $ 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;
- }
- // Obtain the field value
- Public function FieldValue ($ FieldName = ""){
- Return ($ this-> RecordSet [$ this-> mCurrentRow] [$ FieldName]);
- }
- // -------- Move the record set cursor ---------------
- Public function Move ($ RowPos ){
- If ($ RowPos <0) $ RowPos = 0;
- If ($ RowPos> $ this-> RecordCount-1) $ RowPos = $ this-> RecordCount-1;
- $ This-> mCurrentRow = $ RowPos;
- $ This-> EOF = false;
- $ This-> BOF = false;
- }
- Public function MoveNext (){
- If ($ this-> mCurrentRow <$ this-> 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;
- }
- Public function MoveLast (){
- $ This-> mCurrentRow = $ this-> RecordCount-1;
- $ This-> EOF = false;
- $ This-> BOF = false;
- }
- //--------------------------------------------------
- // Used for insert, modify, delete, and other operations
- Public function Execute ($ SqlString ){
- Return @ parent: query ($ SqlString );
- }
- // ----------------- Private function -----------------------------
- // Re-construct an SQL statement. for example, rewrite "select * from tb2" to "select count (*) from tb2" to improve query efficiency.
- 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;
- }
- }
- // ------------- End -----------------------------------
- }
- // ------------- End -----------------------------------
- ?>
2. example: you need to modify the MySQL User name, password, database name, and other information.
-
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 ();
-
?>
|