PHP访问MySql数据库 中级篇 Smarty技术

阅读本文之前,推荐先参阅《PHP访问MySql数据库 初级篇》。

Smarty 是一个使用PHP语言写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,将原本与HTML代码混杂在一起PHP代 码进行了分离。从而使PHP程序员同网站的前端程序员可以达到良好的分工——PHP程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新 修改页面的样式也不会影响到程序的程序逻辑,这使得多人合作的项目变得尤为轻松和易于管理维护。正因为Smarty有这么多的优点,所以国内各大公司在网 站编程时均采用这种编程方法。Smarty的手册可以访问http://www.smarty.net/docs/en/index.tpl

 

下 面是Smarty程序的一个小范例,功能上与初级篇相同——从MySql的test数据库中的t_student读取数据然后显示。程序共分为5个文件, 分别为smarty2.php、smarty2.html、smarty2_head.php、smarty2.js和smarty2.css。此外程序 要引用Smarty和JQuery的库文件。

1.smarty2_head.php文件

  1. <?php  
  2. // by MoreWindows( http://blog.csdn.net/MoreWindows )  
  3. define(DB_HOST, 'localhost');  
  4. define(DB_USER, 'root');  
  5. define(DB_PASS, '111111');  
  6. define(DB_DATABASENAME, 'test');  
  7. define(DB_TABLENAME, 't_student');  
  8.   
  9. $dbcolarray = array('id''name''age');  
  10. ?>  

2.smarty2.php文件

  1. <?php  
  2. // by MoreWindows( http://blog.csdn.net/MoreWindows )  
  3. header("Content-Type: text/html; charset=utf-8");  
  4. require('http://www.cnblogs.com/smart_libs/Smarty.class.php');  
  5. require_once('smarty2_head.php');  
  6. date_default_timezone_set("PRC");  
  7.   
  8. //mysql_connect  
  9. $conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());  
  10. mysql_select_db(DB_DATABASENAME, $conn);  
  11.   
  12. //表中记录条数  
  13. $sql = sprintf("select count(*) from %s", DB_TABLENAME);  
  14. $result = mysql_query($sql$conn);  
  15. if ($result)  
  16. {  
  17.     $dbcount = mysql_fetch_row($result);  
  18.     $tpl_db_count = $dbcount[0];  
  19. }  
  20. else  
  21. {  
  22.     die("query failed");  
  23. }  
  24.   
  25. //表头  
  26. $tpl_db_coltitle = $dbcolarray;  
  27.   
  28. //表中的内容  
  29. $tpl_db_rows = array();  
  30. $sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);  
  31. $result = mysql_query($sql$conn);  
  32. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))//与$row=mysql_fetch_assoc($result)等价  
  33.     $tpl_db_rows[] = $row;  
  34.   
  35. mysql_free_result($result);  
  36. mysql_close($conn);  
  37.   
  38. $tpl = new Smarty;  
  39. $tpl->assign('db_count'$tpl_db_count);  
  40. $tpl->assign('db_coltitle'$tpl_db_coltitle);  
  41. $tpl->assign('db_rows'$tpl_db_rows);  
  42. $tpl->display('smarty2.html');  
  43. ?>  

3.smarty2.html文件

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <link href="smarty2.css" rel="stylesheet" type="text/css" media="all" />  
  5. <script type="text/javascript" src="../jquery-1.7.min.js"></script>  
  6. <script type="text/javascript" src="smarty2.js"></script>  
  7. <title>{$smarty.const.DB_TABLENAME}</title>  
  8. </head>  
  9. <body>  
  10. <h1>表中有{$db_count}条记录</h1>  
  11. <table border="1" align="center" cellpadding="10" cellspacing="2" bordercolor="#ffaaoo">  
  12. {foreach $db_coltitle as $col}  
  13.     <th>{$col}</th>  
  14. {/foreach}  
  15. {foreach $db_rows as $dbrow}  
  16.     <tr>  
  17.     {foreach $dbrow as $k=>$val}  
  18.         <td>{$val}</td>  
  19.     {/foreach}  
  20.     </tr>  
  21. {/foreach}  
  22. </table>  
  23. </body>  
  24. </html>  

4.smarty2.js文件

[javascript] view plaincopy
  1. $(document).ready(function()  
  2. {  
  3.     //用CSS控制奇偶行的颜色  
  4.     $("table tr:odd").css("background-color""#e6e6fa");  
  5.     $("table tr:even").css("background-color""#fff0fa");  
  6. });    

5.smarty2.css文件

  1. @charset "utf-8";  
  2. h1  
  3. {  
  4.     color:Red;  
  5.     text-align:center;  
  6. }  
  7. table th  
  8. {    
  9.     background-color:#7cfc00;    
  10. }   

程序运行结果如下:

 

上例展示了Smarty的基本用法,当然Smarty还提供了更加方便使用的接口函数,如对表格,可以使用{html_table}来快速生成表格。有兴趣的读者可以试试。

现在这个程序再加上《jquery 表格的增加删除和修改及设置奇偶行颜色》中对表格的增加删除和修改功能就基本上可以完善了,请参见下一篇《PHP访问MySql数据库 高级篇 AJAX技术》。

 

 

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/7094642

posted @ 2012-02-03 21:38  永哥  阅读(313)  评论(0)    收藏  举报