PHP PDO 자동 페이징 코드 및 예제

WBOY
풀어 주다: 2016-07-25 08:51:48
원래의
1412명이 탐색했습니다.
  1. /**
  2. * 클래스 이름: PdoPage
  3. * 작성자: Xie Shengtao shishhengsoft@gmail.com
  4. * 설명: PDO 클래스에서 상속되며 MS ADO 구성 요소의 자동 페이징 기능과 유사한 자동 페이징 기능을 추가합니다.
  5. */bbs.it-home.org
  6. //--------- -----시작---------------
  7. class PdoPage 확장 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; / Recordset
  16. private $mCurrentRow = -1; //레코드 세트의 현재 커서 위치
  17. private $Rows = 0;//총 레코드 수
  18. //연결 닫기
  19. public 함수 닫기 (){unset($this );}
  20. //페이징 쿼리
  21. public function QueryEx($SqlString){
  22. // 자동 페이징 기능 활성화 여부
  23. if($this->AutoPage ){
  24. // PageSize 매개변수 확인
  25. if ($this->PageSize <=0) die("경고: PageSize는 음수 또는 0일 수 없습니다.")
  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과 페이지 사이로 제한합니다.
  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)// 재구성 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( );//배열을 반환합니다.
  44. $this->RecordCount = count($this->RecordSet)
  45. if(!$this->AutoPage){$this->Pages = ( !$this->RecordCount)?0:1;}
  46. return $this->RecordCount;
  47. }
  48. // 필드 값 가져오기
  49. 공개 함수 FieldValue($FieldName="" ){
  50. return ($this->RecordSet[$this->mCurrentRow][$FieldName])
  51. }
  52. //---------레코드 세트 커서 이동- - -------------
  53. 공용 함수 Move($RowPos){
  54. if ($RowPos<0) $RowPos = 0
  55. if ($RowPos > $ this ->RecordCount-1) $RowPos = $this->RecordCount-1;
  56. $this->mCurrentRow = $RowPos
  57. $this->EOF = false; - >BOF = false;
  58. }
  59. 공용 함수 MoveNext(){
  60. if($this->mCurrentRow < $this->RecordCount-1){
  61. $this-> ; mCurrentRow;
  62. $this->EOF = false;
  63. }
  64. else{
  65. $this->EOF = true; > }
  66. }
  67. 공용 함수 MovePrev(){
  68. if($this->mCurrentRow > 0){
  69. $this->mCurrentRow--; ; EOF = false;
  70. $this->BOF = false;
  71. $this->BOF =
  72. }
  73. }
  74. 공개 함수 ) {
  75. $this->mCurrentRow = 0;
  76. $this->EOF = false;
  77. $this->BOF = false
  78. }
  79. 공용 함수 MoveLast() {
  80. $this->mCurrentRow = $this->RecordCount-1;
  81. $this->EOF = false
  82. $this->BOF =
  83. } > //---------------------------------- - --
  84. // 삽입, 수정, 삭제 및 기타 작업을 수행하는 데 사용됩니다.
  85. public function Execute($SqlString){
  86. return @parent::query($SqlString)
  87. }
  88. / /----개인 기능---------------------------- -
  89. // 쿼리 효율성을 높이기 위해 "select * from tb2"를 "select count(*) from tb2"로 다시 작성하는 등 SQL 문을 재구성합니다.
  90. 비공개 함수 buildSqlString($SqlString){
  91. if(preg_match("/select[ ,./w /*] from/",$SqlString,$marr)){
  92. $columns = preg_replace(" /select|from/","",$marr[0]);
  93. $columns = preg_replace("//*/","/*",$columns);
  94. $result = preg_replace(" /$columns/"," count(*) ",$SqlString);
  95. return $result;
  96. }
  97. }
  98. //------------- 종료-----------------------------------
  99. }
  100. //------ - -------끝---------
  101. ?
  102. 코드 복사
  103. 2. 사용 예: MySQL 사용자 이름, 비밀번호, 데이터베이스 이름 및 기타 정보를 수정해야 합니다.

    1. <

    2. include_once("./pdopage_class.php")
    3. $db = new PdoPage ("mysql:host=localhost;dbname=mydb","root","123456")
    4. $db->Execute("문자 집합 gbk;"); = false;
    5. $db->PageSize = 6;
    6. $db->QueryEx("select * from tb2;"); db->MoveFirst();
    7. while (!$db->EOF) {
    8. echo $db->FieldValue("id"),"/t",$db->FieldValue( "이름"),"/t",$db->FieldValue("나이"),"/n"
    9. $db->MoveNext()
    10. $db-> ;닫기();

    11. ?>

    12. 코드 복사
    >
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!