当前位置:Gxlcms > PHP教程 > phppdo自动分页类代码与例子

phppdo自动分页类代码与例子

时间:2021-07-01 10:21:17 帮助过:24人阅读

  1. /**
  2. * 类名: PdoPage
  3. * 作者:谢声涛 shishengsoft@gmail.com
  4. * 描述: 继承自PDO类,增加了自动分页功能,类似MS ADO组件的自动分页功能。
  5. */ bbs.it-home.org
  6. //-------------开始---------------
  7. class PdoPage extends PDO {
  8. public $RecordCount = 0; // 记录集的记录总数
  9. public $AutoPage = false;// 启用自动分页功能
  10. public $PageSize = 0;// 每页的记录行数
  11. public $CurrentPage = 0; // 当前页
  12. public $Pages = 0;// 总页数
  13. public $BOF = false; // 游标到记录集之前
  14. public $EOF = false; // 游标到记录集之后
  15. private $RecordSet = null; // 记录集
  16. private $mCurrentRow = -1; // 记录集中当前游标位置
  17. private $Rows = 0;//总记录数
  18. // 关闭连接
  19. public function Close(){unset($this);}
  20. // 分页查询
  21. public function QueryEx($SqlString){
  22. // 是否启用自动分页功能
  23. if($this->AutoPage){
  24. // 检查PageSize参数
  25. if ($this->PageSize <=0) die("警告:PageSize不能为负数或零。");
  26. // 计算总记录数
  27. $rs = @parent::query($this->rebuildSqlString($SqlString));
  28. $this->Rows = $rs->fetchColumn();
  29. // 计算总页数
  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. // 约束CurrentPage值,使之位于1到Pages之间。
  34. if($this->CurrentPage < 1) {$this->CurrentPage =1;}
  35. if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}
  36. //计算偏移量
  37. $Offset = $this->PageSize * ($this->CurrentPage - 1);
  38. // 重组SQL语句,SqlString有分号则去掉
  39. $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";
  40. }
  41. // 查询并返回记录集
  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. // 取得字段值
  50. public function FieldValue($FieldName=""){
  51. return ($this->RecordSet[$this->mCurrentRow][$FieldName]);
  52. }
  53. //--------移动记录集游标---------------
  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. // 用于执行插入、修改、删除等操作
  92. public function Execute($SqlString){
  93. return @parent::query($SqlString);
  94. }
  95. //-----------------私有函数-----------------------------
  96. // 重新构造SQL语句,如将"select * from tb2"改写为"select count(*) from tb2",旨在提高查询效率。
  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. //-------------结束-----------------------------------
  106. }
  107. //-------------结束-----------------------------------
  108. ?>

2、使用示例: 需修改MySQL用户名、密码、数据库名等信息。

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

人气教程排行