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

php smarty分頁原理與分頁程式碼分享

WBOY
WBOY原創
2016-07-25 08:52:411017瀏覽
  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. {if $isbbs=="t"}
  3. 表>
  4. {/if}
  5. {if $isbbstell=="f" && $isbbs=="f" }
  6. < ;tr>
  7. table>
  8. {/if}
  9. > ;
  10. php smarty分頁原理與分頁程式碼分享
  11. body>
  12. 陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
  13. {php}
  14. $i=1;
  15. {/php}
  16. {section name=bbsid loop=$arraybbs}
  17. {php}
  18. $i ;
  19. {/php}
  20. {/section}
  21. 图书名称
    图书内容
    出版日期
    图书作者
    {unhtml content=$arraybbs[bbsid].bookname}
    {$arraybbs[bbsid].bookintro}
    {$arraybbs[bbsid].booktime}
    {$arraybbs[bbsid].bookauthor}
  22.  {$showpage}
    {ldelim}暫時沒有內容{rdelim}字體>