pdo分页类

  1. 1、新建一个PHP文件,文件名为pdopage_class.php,将以下代码粘贴进去。  
  2.   
  3.   
  4.   
  5. 代码如下:   
  6.   
  7.   
  8.   
  9. <?php  
  10.   
  11. /** 
  12.  
  13.  * 类名: PdoPage 
  14.  
  15.  * 作者:谢声涛 [email protected] 
  16.  
  17.  * 描述: 继承自PDO类,增加了自动分页功能,类似MS ADO组件的自动分页功能。 
  18.  
  19.  */  
  20.   
  21. //-------------开始-----------------------------------  
  22.   
  23. class PdoPage extends PDO {      
  24.   
  25.  public $RecordCount = 0;    // 记录集的记录总数  
  26.   
  27.  public $AutoPage = false;   // 启用自动分页功能  
  28.   
  29.  public $PageSize = 0;       // 每页的记录行数  
  30.   
  31.  public $CurrentPage = 0;    // 当前页  
  32.   
  33.  public $Pages = 0;          // 总页数  
  34.   
  35.  public $BOF = false;        // 游标到记录集之前  
  36.   
  37.  public $EOF = false;        // 游标到记录集之后  
  38.   
  39.    
  40.   
  41.  private $RecordSet = null;  // 记录集   
  42.   
  43.  private $mCurrentRow = -1;  // 记录集中当前游标位置  
  44.   
  45.  private $Rows = 0;          //总记录数  
  46.   
  47.    
  48.   
  49.  // 关闭连接  
  50.   
  51.  public function Close(){unset($this);}   
  52.   
  53.  // 分页查询  
  54.   
  55.  public function QueryEx($SqlString){  
  56.   
  57.   // 是否启用自动分页功能  
  58.   
  59.   if($this->AutoPage){  
  60.   
  61.    // 检查PageSize参数  
  62.   
  63.    if ($this->PageSize <=0) die("警告:PageSize不能为负数或零。");  
  64.   
  65.    // 计算总记录数  
  66.   
  67.    $rs = @parent::query($this->rebuildSqlString($SqlString));  
  68.   
  69.    $this->Rows = $rs->fetchColumn();   
  70.   
  71.    // 计算总页数  
  72.   
  73.    if ($this->Rows < $this->PageSize) {$this->Pages = 1;}  
  74.   
  75.    elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this->PageSize)+1;}  
  76.   
  77.    else {$this->Pages = intval($this->Rows/$this->PageSize);}  
  78.   
  79.    // 约束CurrentPage值,使之位于1到Pages之间。  
  80.   
  81.    if($this->CurrentPage < 1) {$this->CurrentPage =1;}  
  82.   
  83.    if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}    
  84.   
  85.    //计算偏移量  
  86.   
  87.    $Offset = $this->PageSize * ($this->CurrentPage - 1);  
  88.   
  89.    // 重组SQL语句,SqlString有分号则去掉  
  90.   
  91.    $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";  
  92.   
  93.   }  
  94.   
  95.   // 查询并返回记录集  
  96.   
  97.   $rs = new PDOStatement();  
  98.   
  99.   $rs = @parent::query($SqlString);  
  100.   
  101.   $this->RecordSet = $rs->fetchAll();//returns an array.  
  102.   
  103.   $this->RecordCount = count($this->RecordSet);  
  104.   
  105.   if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}  
  106.   
  107.   return $this->RecordCount;  
  108.   
  109.  }   
  110.   
  111.  // 取得字段值  
  112.   
  113.  public function FieldValue($FieldName=""){  
  114.   
  115.   return ($this->RecordSet[$this->mCurrentRow][$FieldName]);  
  116.   
  117.  }   
  118.   
  119.  //--------移动记录集游标---------------------------------------------  
  120.   
  121.  public function Move($RowPos){  
  122.   
  123.   if ($RowPos<0) $RowPos = 0;  
  124.   
  125.   if ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;  
  126.   
  127.   $this->mCurrentRow = $RowPos;  
  128.   
  129.   $this->EOF = false;  
  130.   
  131.   $this->BOF = false;    
  132.   
  133.  }  
  134.   
  135.  public function MoveNext(){  
  136.   
  137.   if($this->mCurrentRow < $this->RecordCount-1){  
  138.   
  139.    $this->mCurrentRow++;     
  140.   
  141.    $this->EOF = false;  
  142.   
  143.    $this->BOF = false;  
  144.   
  145.   }  
  146.   
  147.   else{  
  148.   
  149.    $this->EOF = true;  
  150.   
  151.   }  
  152.   
  153.  }  
  154.   
  155.  public function MovePrev(){  
  156.   
  157.   if($this->mCurrentRow > 0){  
  158.   
  159.    $this->mCurrentRow--;  
  160.   
  161.    $this->EOF = false;  
  162.   
  163.    $this->BOF = false;  
  164.   
  165.   }else{  
  166.   
  167.    $this->BOF = true;  
  168.   
  169.   }   
  170.   
  171.  }  
  172.   
  173.  public function MoveFirst(){  
  174.   
  175.   $this->mCurrentRow = 0;  
  176.   
  177.   $this->EOF = false;  
  178.   
  179.   $this->BOF = false;  
  180.   
  181.  }   
  182.   
  183.  public function MoveLast(){  
  184.   
  185.   $this->mCurrentRow = $this->RecordCount-1;  
  186.   
  187.   $this->EOF = false;  
  188.   
  189.   $this->BOF = false;  
  190.   
  191.  }  
  192.   
  193.  //--------------------------------------------------  
  194.   
  195.  // 用于执行插入、修改、删除等操作  
  196.   
  197.  public function Execute($SqlString){  
  198.   
  199.   return @parent::query($SqlString);  
  200.   
  201.  }  
  202.   
  203.  //-----------------私有函数-----------------------------  
  204.   
  205.  // 重新构造SQL语句,如将"select * from tb2"改写为"select count(*) from tb2",旨在提高查询效率。  
  206.   
  207.  private function rebuildSqlString($SqlString){  
  208.   
  209.   if(preg_match("/select[ ,./w+/*]+ from/",$SqlString,$marr)){    
  210.   
  211.    $columns = preg_replace("/select|from/","",$marr[0]);   
  212.   
  213.    $columns = preg_replace("//*/","/*",$columns);  
  214.   
  215.    $result = preg_replace("/$columns/"," count(*) ",$SqlString);   
  216.   
  217.    return $result;  
  218.   
  219.   }  
  220.   
  221.  }   
  222.   
  223.  //-------------结束-----------------------------------  
  224.   
  225. }  
  226.   
  227. //-------------结束-----------------------------------  
  228.   
  229.   
  230.   
  231. ?>  
  232.   
  233.   
  234.   
  235. 2、使用示例:  
  236.   
  237.   
  238.   
  239. 请根据自己的情况修改MySQL用户名、密码、数据库名等信息。  
  240.   
  241.   
  242.   
  243. <?  
  244.   
  245. include_once("./pdopage_class.php");  
  246.   
  247.   
  248.   
  249. $db = new PdoPage("mysql:host=localhost;dbname=mydb","root","123456");  
  250.   
  251. $db->Execute("set character set gbk;");  
  252.   
  253.   
  254.   
  255. $db->AutoPage = false;  
  256.   
  257. $db->PageSize = 6;  
  258.   
  259. $db->CurrentPage = 1;  
  260.   
  261. $db->QueryEx("select * from tb2;");  
  262.   
  263.   
  264.   
  265. $db->MoveFirst();  
  266.   
  267. while (!$db->EOF) {  
  268.   
  269.  echo  $db->FieldValue("id"),"/t",$db->FieldValue("name"),"/t",$db->FieldValue("age"),"/n";  
  270.   
  271.  $db->MoveNext();  
  272.   
  273. }  
  274.   
  275.   
  276.   
  277. $db->Close();  
  278.   
  279.   
  280.   
  281. ?>  

猜你喜欢

转载自blog.csdn.net/qq_42030417/article/details/80276505
PDO