首页 > 后端开发 > php教程 > php pdo自动分页类代码与例子

php pdo自动分页类代码与例子

WBOY
发布: 2016-07-25 08:51:48
原创
1483 人浏览过
  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 // 计算总记录数
  26. $rs = @parent::query($this->rebuildSqlString($SqlString));
  27. $this->Rows = $rs->fetchColumn();
  28. // 计算总页数
  29. if ($this->Rows PageSize) {$this->Pages = 1;}
  30. elseif ($this->Rows % $this->PageSize) {$this->Pages = intval($this->Rows/$this->PageSize)+1;}
  31. else {$this->Pages = intval($this->Rows/$this->PageSize);}
  32. // 约束CurrentPage值,使之位于1到Pages之间。
  33. if($this->CurrentPage CurrentPage =1;}
  34. if($this->CurrentPage > $this->Pages) {$this->CurrentPage = $this->Pages;}
  35. //计算偏移量
  36. $Offset = $this->PageSize * ($this->CurrentPage - 1);
  37. // 重组SQL语句,SqlString有分号则去掉
  38. $SqlString = str_replace(";","",$SqlString) . " LIMIT $Offset,$this->PageSize;";
  39. }
  40. // 查询并返回记录集
  41. $rs = new PDOStatement();
  42. $rs = @parent::query($SqlString);
  43. $this->RecordSet = $rs->fetchAll();//returns an array.
  44. $this->RecordCount = count($this->RecordSet);
  45. if(!$this->AutoPage){$this->Pages = (!$this->RecordCount)?0:1;}
  46. return $this->RecordCount;
  47. }
  48. // 取得字段值
  49. public function FieldValue($FieldName=""){
  50. return ($this->RecordSet[$this->mCurrentRow][$FieldName]);
  51. }
  52. //--------移动记录集游标---------------
  53. public function Move($RowPos){
  54. if ($RowPosif ($RowPos > $this->RecordCount-1) $RowPos = $this->RecordCount-1;
  55. $this->mCurrentRow = $RowPos;
  56. $this->EOF = false;
  57. $this->BOF = false;
  58. }
  59. public function MoveNext(){
  60. if($this->mCurrentRow RecordCount-1){
  61. $this->mCurrentRow++;
  62. $this->EOF = false;
  63. $this->BOF = false;
  64. }
  65. else{
  66. $this->EOF = true;
  67. }
  68. }
  69. public function MovePrev(){
  70. if($this->mCurrentRow > 0){
  71. $this->mCurrentRow--;
  72. $this->EOF = false;
  73. $this->BOF = false;
  74. }else{
  75. $this->BOF = true;
  76. }
  77. }
  78. public function MoveFirst(){
  79. $this->mCurrentRow = 0;
  80. $this->EOF = false;
  81. $this->BOF = false;
  82. }
  83. public function MoveLast(){
  84. $this->mCurrentRow = $this->RecordCount-1;
  85. $this->EOF = false;
  86. $this->BOF = false;
  87. }
  88. //--------------------------------------------------
  89. // 用于执行插入、修改、删除等操作
  90. public function Execute($SqlString){
  91. return @parent::query($SqlString);
  92. }
  93. //-----------------私有函数-----------------------------
  94. // 重新构造SQL语句,如将"select * from tb2"改写为"select count(*) from tb2",旨在提高查询效率。
  95. private function rebuildSqlString($SqlString){
  96. if(preg_match("/select[ ,./w+/*]+ from/",$SqlString,$marr)){
  97. $columns = preg_replace("/select|from/","",$marr[0]);
  98. $columns = preg_replace("//*/","/*",$columns);
  99. $result = preg_replace("/$columns/"," count(*) ",$SqlString);
  100. return $result;
  101. }
  102. }
  103. //-------------结束-----------------------------------
  104. }
  105. //-------------结束-----------------------------------
  106. ?>
复制代码

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

复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板