Heim > Backend-Entwicklung > PHP-Tutorial > php pdo自动分页类代码与例子

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

WBOY
Freigeben: 2016-07-25 08:51:48
Original
1483 Leute haben es durchsucht
  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. ?>

复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage