PHP 스마트 페이징 원리 및 페이징 코드 공유

WBOY
풀어 주다: 2016-07-25 08:52:41
원래의
985명이 탐색했습니다.
    1. include_once("smarty.class.php");

    2. class smartyproject extendssmarty
    3. {
    4. 함수 __construct()
    5. {

    6. $this->config_dir="smarty/smarty/config_file.class.php";

    7. $this->caching=false;
    8. $this->template_dir = "smarty/templates/";
    9. $this->cache_dir = "smarty/smarty_cache/";
    10. $ this->left_delimiter = "{";
    11. $this->right_delimiter = "}";
    12. }
    13. }
    14. ?>

    复主代码

    2,adodb,连接数据库类:

    1. class conndb
    2. {
    3. var $dbtype;
    4. var $host;
    5. var $user;
    6. var $ pwd;
    7. var $dbname;
    8. var $debug;//false不显示侦错信息,反之,显示
    9. var $conn;
    10. function __construct($dbtype,$host,$user, $pwd,$dbname,$debug=false)
    11. {
    12. $this->dbtype = $dbtype;
    13. $this->host = $host;
    14. $this->user = $user;
    15. $this->pwd = $pwd;
    16. $this->dbname = $dbname;
    17. $this->debug = $debug;
    18. }
    19. //사용 가능한 정보(个人理解可以说返回adodb对象)
    20. function getconnid()
    21. {
    22. include_once('adodb5/adodb.inc.php');
    23. 만약에 ($this->dbtype == "mysql" || $this->dbtype == "mssql")
    24. {
    25. if($this->dbtype == "mysql")
    26. {
    27. $this->conn = newadoconnection("mysql");//创建adodb对象,声明数据库类型为mysql
    28. }
    29. else
    30. {
    31. $this->conn = newadoconnection("mssql");
    32. }
    33. $this->conn->connect($this->host,$this->user,$this->pwd,$this- >dbname);
    34. }
    35. else if($this->dbtype == "access")
    36. {
    37. $this->conn = newadoconnection("access") ;
    38. $this->conn->connect("driver={microsoft 액세스 드라이버(*.mdb)};dbq=".$this->dbname.";uid=".$this-> ;user.";pwd=".$this->pwd.";");
    39. }
    40. $this->conn->execute("이름 설정 utf-8") ;
    41. if($this->dbtype == "mysql")
    42. {
    43. $this->conn->debug = $this->debug;
    44. }
    45. return $this->conn;
    46. }
    47. function closeconnid()//关闭与数据库的连接
    48. {
    49. $this->conn->disconnection( );
    50. }
    51. }
    复主代码

    3,数据库操작类:

    1. class admindb
    2. {
    3. var $sqlstr;
    4. var $conn;
    5. var $sqltype;
    6. var $ rs;
    7. var $array;
    8. 함수 execsql($sqlstr,$conn)
    9. {
    10. $rs = $conn->execute($sqlstr);
    11. $sqltype = strtolower( substr(trim($sqlstr),0,6)); //strtolower() 函数把字符串转换为小写。
    12. if($sqltype = "select")
    13. {
    14. $array = $rs->getrows(); // 类似mysql_fetch_array函数返回的数组
    15. if(count($array) == 0 || $rs == false)
    16. {
    17. return false;
    18. }
    19. else
    20. {
    21. $array 반환;
    22. }
    23. }
    24. else if($sqltype = "update"||$sqltype = "insert"||$sqltype = "delete")
    25. {
    26. if($rs)
    27. {
    28. true를 반환;
    29. }
    30. else
    31. {
    32. false를 반환;
    33. }
    34. }
    35. }
    36. }
    复代码

    4, 구분:

    1. 클래스 페이지

    2. {
    3. var $rs;
    4. var $pagesize;
    5. var $ nowpage;
    6. var $array;
    7. var $conn;
    8. var $sqlstr;
    9. function showdate($sqlstr,$conn,$pagesize,$nowpage)
    10. {
    11. if( !isset($nowpage)||$nowpage=="")
    12. {
    13. $nowpage = 1 ;
    14. }
    15. else
    16. {
    17. $this->nowpage = $ nowpage;
    18. }
    19. $this->pagesize = $pagesize;
    20. $this->conn = $conn;
    21. $this->sqlstr = $sqlstr;
    22. $this ->rs = $this->conn->pageexecute($this->sqlstr,$this->pagesize,$this->nowpage);
    23. //pageexecute($sql, $nrows , $page, $inputarr=false) 使用资料集的页码功能,叁数 $page 是以 1 为启使值
    24. $this->array = $this->rs->getrows();
    25. if(count($this->array) == 0 || $this->rs == false)
    26. {
    27. return false;
    28. }
    29. else
    30. {
    31. return $this->array;
    32. }
    33. }

    34. function showpage($contentname,$utits,$anothersearchstr,$class)

    35. {

    36. $allrs=$this->conn->execute($this->sqlstr);

    37. $record=count($allrs->getrows());
    38. $pagecount=ceil($record/$this->pagesize);

    39. $str.="共有".$contentname." ". $record." ".$utits." 每页显示 ".$this->pagesize." ".$utits." 第 ".$this->rs ->absolutepage()." 页/共 ".$pagecount." 页";

    40. $str.="    ";

    41. if(!$this->rs->atfirstpage())

    42. {
    43. $str.="首页";
    44. }
    45. else
    46. {
    47. $str.="首页";
    48. }
    49. $str.=" ";

    50. if(!$this->rs->atfirstpage())

    51. {
    52. $str.="上一页";
    53. }
    54. else
    55. {
    56. $str.="上一页";
    57. }
    58. $str.=" ";
    59. if(!$this->rs->atlastpage())

    60. {
    61. $str.="下一页";
    62. }
    63. else
    64. {
    65. $str.="下一页";
    66. }
    67. $str.=" ";
    68. if(!$this->rs->atlastpage())

    69. {
    70. $str.="尾页";
    71. }
    72. else
    73. {
    74. $str.="尾页";
    75. }
    76. if(count($this->array)==0 || $this->rs==false)
    77. {
    78. return "";
    79. }
    80. else
    81. {
    82. return $str;
    83. }
    84. }
    85. }

    复代码

    5. 기사 문자 변환 처리를 위한 클래스:

    1. class usefun

    2. {
    3. function unhtml($text)
    4. {
    5. $content=(nl2br(htmlspecialchars($text)));//htmlspecialchars() 함수는 미리 정의된 일부 문자를 html 엔터티로 변환하고, nl2br() 함수는 문자열의 각 새 줄(/n) 앞에 삽입합니다. html 개행 문자(< br />).
    6. $content=str_replace("[strong]","$content=str_replace("[/strong]"," ",$content);

    7. $content=str_replace("[em]","",$content);
    8. $content=str_replace("[/em]" ,"",$content);
    9. $content=str_replace("[u]","",$content);
    10. $content=str_replace("[/u ]","",$content);
    11. $content=str_replace("[font color=#ff0000]","", $content);
    12. $content=str_replace("[font color=#00ff00]","",$content);
    13. $content=str_replace("[font color= #0000ff]","",$content);
    14. $content=str_replace("[fontface=楷体_gb2312]","",$content);
    15. $content=str_replace("[fontface=宋体]","",$content);
    16. $content=str_replace(" [글꼴 얼굴=공식 스크립트]","<글꼴 얼굴=공식 스크립트>",$content);
    17. $content=str_replace("[/font]","",$content) ;
    18. //$content=str_replace(chr(32)," ",$content);
    19. $content=str_replace("[font size=1]","", $ content);
    20. $content=str_replace("[font size=2]","",$content);
    21. $content=str_replace("[font size=3] " ,"",$content);
    22. $content=str_replace("[font size=4]","",$content);
    23. $ content=str_replace("[글꼴 크기=5]","<글꼴 크기=5>",$content);
    24. $content=str_replace("[글꼴 크기=6]","<글꼴 크기 = 6>",$content);

    25. $content=str_replace("[fieldset][legend]","

      ",$content);
    26. $content=str_replace("[/legend]","",$content);
    27. $content=str_replace("[/fieldset]","", $ content);
    28. return $content;
    29. }
    30. }

    코드 복사

    위 생성 네 개의 모든 클래스는 하나의 클래스 파일 system.class.inc.php에 배치됩니다.

    몇 가지 추가 파일: 1. 시스템.inc.php:

    1. session_start();

    2. include_once("smarty_inc.php");
    3. include_once("system.class.inc.php");
    4. //데이터베이스 연결 클래스 인스턴스화
    5. $connobj = new conndb("mysql","localhost","root","vertrigo"," db_fenye ",false);
    6. $conn = $connobj->getconnid();
    7. //데이터베이스 작업 클래스 인스턴스화
    8. $admindb = new admindb();
    9. //페이징 클래스 인스턴스
    10. $seppage=new seppage();
    11. //인스턴스화를 위해 공통 함수 클래스 사용
    12. $usefun=new usefun();
    13. //smarty 템플릿 호출
    14. $smarty=new smartyproject ();
    15. 함수 unhtml($params)
    16. {
    17. extract($params);
    18. $text=$content;
    19. global $usefun;
    20. return $usefun-> text);
    21. }

    22. $smarty->register_function("unhtml","unhtml");

    23. ?>

    코드 복사

    2、执行文件、index.php:

    1. include_once("system.inc.php");
    2. $arraybbstell = $admindb->execsql(" tb_bookinfo",$conn)에서 * 선택;
    3. if(!$arraybbstell)
    4. {
    5. $smarty->ass("isbbstell","t");
    6. }
    7. else
    8. {
    9. $smarty->할당("isbbstell",t);
    10. $smarty->할당("arraybbstell",$arraybbstell);
    11. }
    12. $arraybbs = $ seppage->showdate("select * from tb_bookinfo",$conn,1,$_get["page"]);
    13. if(!$arraybbs)
    14. {
    15. $smarty->할당( "isbbs",f);
    16. }
    17. else
    18. {
    19. $smarty->ass("isbbs",t);
    20. $smarty->assip(" showpage",$seppage->showpage("帖子","条","","a1"));
    21. $smarty->asse("arraybbs",$arraybbs);
    22. }
    23. $smarty->display("index.html");
    24. ?>
    复system代码

    3、解析文件,index.html:

    1. 页模
    2. {if $isbbs=="t"}
    3. {php}
    4. $i=1;
    5. {/php}
    6. {section name=bbsid loop=$arraybbs}
    7. {php}
    8. $i ;
    9. {/php}
    10. {/섹션}
    11. 图书名称
      图书内容
      出版日期
      图书作者
      {unhtml content=$arraybbs[bbsid].bookname}
      {$arraybbs[bbsid].bookintro}
      {$arraybbs[bbsid].booktime}
      {$arraybbs[bbsid].bookauthor}
    12. <표 너비 ="96%" height="5" border="0" align="center" cellpadding="0" cellpacing="0">
    13. < 테이블 너비="100%" 높이="22" border="0" align="center" cellpadding="0" cellpacing="0">
    14.  {$showpage}
    15. {/if}
    16. {if $isbbstell=="f" && $isbbs=="f" }
    17. < ;tr>
    18. {ldelim}暂时没有内容{rdelim}
    19. {/if}
     
  • 复主代码

    总结: 需切记smarty,adodb文件引入的愛关一些路径问题。 에서 执行文件index.html中적 css路径, 图 Images背景路径city是상对于index.php这个执行文件的 (如css/css.cs와index.php에서 같은 일级), 引入方法是:

    adodb의 분할页函数:pageexecute,absolutepage()当前页码,atfirstpage()首页页码,atlastpage()尾页页码。(个人理解) $_server['php_self']:当前执行文件。 smarty의register_function()参考另文。



    관련 라벨:
    원천:php.cn
    이전 기사:PHP 정적 변수를 캐시로 사용하는 방법에 대한 예제 튜토리얼 다음 기사:PHP 페이징 클래스 사용법(yii 프레임워크에서 추출)
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    저자별 최신 기사
    최신 이슈
    관련 주제
    더>
    인기 추천
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!