-
- /**
- * Class name: PdoPage
- * Author: Xie Shengtao shishengsoft@gmail.com
- * Description: Inherited from the PDO class, it adds automatic paging function, similar to the automatic paging function of MS ADO components.
- */ bbs.it-home.org
- //-------------Start-------- -------
- class PdoPage extends PDO {
- public $RecordCount = 0; // Total number of records in the recordset
- public $AutoPage = false;// Enable automatic paging
- public $PageSize = 0;// Each Number of record rows in the page
- public $CurrentPage = 0; // Current page
- public $Pages = 0; // Total number of pages
- public $BOF = false; // Before the cursor reaches the record set
- public $EOF = false; / / After the cursor reaches the record set
- private $RecordSet = null; // Record set
- private $mCurrentRow = -1; // Current cursor position in the record set
- private $Rows = 0; //Total number of records
- // Close the connection
- public function Close(){unset($this);}
- //Paging query
- public function QueryEx($SqlString){
- //Whether the automatic paging function is enabled
- if($this->AutoPage){
- //Check 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 between.
- 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, remove the semicolon in SqlString
- $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;
- }
- // Get the field value
- public function FieldValue($FieldName=""){
- return ($this->RecordSet[$this->mCurrentRow][$FieldName]);
- }
- //---- ----Move 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 to perform insertion, modification, deletion, etc. Operation
- public function Execute($SqlString){
- return @parent::query($SqlString);
- }
- //-----------------Private function---- --------------------------
- // Reconstruct the SQL statement, such as rewriting "select * from tb2" to "select count(*) from tb2", designed 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------------ -----------------------
- ?>
Copy code
2. Usage examples:
It is necessary 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();
?>
-
Copy code
|