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

WBOY
Freigeben: 2016-07-25 08:52:41
Original
986 Leute haben es durchsucht
  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]","
  8. ",$content);

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

    ",$content);
  31. $content=str_replace("[/legend]","
  32. ",$content);
  33. $content=str_replace("[/fieldset]","
  34. ",$content);
  35. return $content;
  36. }
  37. }
复制代码

将以上四个类全部放到一个类文件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. php+smarty分页原理与分页代码分享
  3.  
    php+smarty分页原理与分页代码分享
  4.  
  5. {if $isbbs=="t"}
  6.   
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
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!