caching=false; $this->template_dir = "smarty/templates/"; $thi">
当前位置:Gxlcms > PHP教程 > php+smarty分页原理与分页代码分享

php+smarty分页原理与分页代码分享

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

  1. include_once("smarty.class.php");

  2. class smartyproject extendssmarty
  3. {
  4. function __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. if($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 access driver (*.mdb)};dbq=".$this->dbname.";uid=".$this->user.";pwd=".$this->pwd.";");
  39. }
  40. $this->conn->execute("set names 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. function 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. return $array;
  22. }
  23. }
  24. else if($sqltype = "update"||$sqltype = "insert"||$sqltype = "delete")
  25. {
  26. if($rs)
  27. {
  28. return true;
  29. }
  30. else
  31. {
  32. return false;
  33. }
  34. }
  35. }
  36. }

4,分页类:

  1. class seppage

  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.="rs->absolutepage()-1).$anothersearchstr." class=".$class.">上一页";
  53. }
  54. else
  55. {
  56. $str.="上一页";
  57. }
  58. $str.=" ";

  59. if(!$this->rs->atlastpage())

  60. {
  61. $str.="rs->absolutepage()+1).$anothersearchstr." class=".$class.">下一页";
  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 换行符 (
    )。

  6. $content=str_replace("[strong]","",$content);

  7. $content=str_replace("[/strong]","",$content);
  8. $content=str_replace("[em]","",$content);
  9. $content=str_replace("[/em]","",$content);
  10. $content=str_replace("[u]","",$content);
  11. $content=str_replace("[/u]","",$content);
  12. $content=str_replace("[font color=#ff0000]","",$content);
  13. $content=str_replace("[font color=#00ff00]","",$content);
  14. $content=str_replace("[font color=#0000ff]","",$content);
  15. $content=str_replace("[font face=楷体_gb2312]","",$content);
  16. $content=str_replace("[font face=宋体]","",$content);
  17. $content=str_replace("[font face=隶书]","",$content);
  18. $content=str_replace("[/font]","",$content);
  19. //$content=str_replace(chr(32)," ",$content);
  20. $content=str_replace("[font size=1]","",$content);
  21. $content=str_replace("[font size=2]","",$content);
  22. $content=str_replace("[font size=3]","",$content);
  23. $content=str_replace("[font size=4]","",$content);
  24. $content=str_replace("[font size=5]","",$content);
  25. $content=str_replace("[font size=6]","",$content);

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

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

将以上四个类全部放到一个类文件system.class.inc.php里.

另外几个文件: 1、system.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. function unhtml($params)
  16. {
  17. extract($params);
  18. $text=$content;
  19. global $usefun;
  20. return $usefun->unhtml($text);
  21. }

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

  23. ?>

2、执行文件,index.php:

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

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. {/section}
  11. 图书名称图书内容出版日期 图书作者
    {unhtml content=$arraybbs[bbsid].bookname}{$arraybbs[bbsid].bookintro}{$arraybbs[bbsid].booktime}{$arraybbs[bbsid].bookauthor}
  12. {$showpage}
  13. {/if}
  14. {if $isbbstell=="f" && $isbbs=="f"}
  15. {ldelim}暂时没有内容{rdelim}
  16. {/if}

总结: 需切记smarty,adodb文件引入的相关一些路径问题。 在执行文件index.html中的css路径,图片背景路径都是相对于index.php这个执行文件的(如css/css.cs和index.php在同一级),引入方法是:

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

人气教程排行