搜尋
首頁後端開發php教程php smarty分頁原理與分頁程式碼分享

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

  2. class smartyproject extendmartssy
  3. >函數__construct()
  4. {
  5. $this->config_dir="smarty/smarty/config_file.class.php";

  6. $this->caching=false;
  7. $this->template_dir = "smarty/templates/";
  8. $this->cache_dir = "smarty/smarty_cache/";
  9. $ this->left_delimiter = "{";
  10. $this-🎜> $this- >right_delimiter = "}";
  11. }
  12. }
  13. ?>
複製程式碼

複製程式碼

2,dbdb ,連接資料庫類別:
  1. class conndb
  2. {
  3. var $dbtype;
  4. var $host;
  5. var $user
  6. var $🎜>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. $this->conn = newadoconnection("mssql");
  33. }
  34. $this->conn->connect($this->host,$this->user,$this->pwd,$this- >dbname);
  35. }
  36. else if ($this->dbtype == "access")
  37. {
  38. $this->conn = newadoconnection("access") ;
  39. $this->conn->connect("driver={microsoft access driver (*.mdb)};dbq=".$this->dbname.";uid=".$this-> ;user.";pwd=".$this->pwd.";");
  40. }
  41. $this->conn->execute("設定名稱utf-8") ;
  42. if($this->dbtype == "mysql")
  43. {
  44. $this->conn->debug = $this->debug;
  45. }
  46. return $this->conn;
  47. }
  48. function closeconnid()//關機與資料庫的連接
  49. {
$this->conn->disconnection( );
}}

複製程式碼

    複製程式碼
  1. 3,資料庫操作類:
  2. class admindb
  3. {
  4. var $sqlstr;
  5. var $conn;
  6. var $sqltype;
  7. var $ rs;
  8. var $array;
  9. function execsql($sqlstr,$conn)
  10. {
  11. $rs = $conn->execute($sqlstr);
  12. $sqltype = strtolower( substrtolower( substr (修剪($sqlstr),0,6)); //strtolower() 函數把字串轉換為小寫。
  13. if($sqltype = "select")
  14. {
  15. $array = $rs->getrows(); // 類似mysql_fetch_array函數傳回的吞吐量
  16. if(count($array) = = 0 || $rs == false)
  17. {
  18. return false;
  19. }
  20. else
  21. {
  22. return $array;
  23. }
  24. }
  25. return $array;
  26. }
  27. }
  28. }
  29. else if($sqltype = "更新"||$sqltype = "插入"||$sqltype = "刪除")
  30. {
  31. if($rs)
{
回傳true; } else
{回傳false; }} } } 複製程式碼 複製程式碼 複製程式碼 複製碼🎜>

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); p>
  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.=" "; p>
  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. return $str;
  86. }
}
}
複製程式碼

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("smarty_inc.php"); ("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 global $usefun;
  21. return $usefun->unhtml($text );
  22. }
  23. $smarty->register_function("unhtml","unhtml");

  24. ?>
複製程式碼

2、執行文件,index.php:

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

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

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。