搜索
首页后端开发php教程php+smarty分页原理与分页代码分享

  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.   
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)