閱讀本文之前,推薦先參閱《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文件
<?php
// by MoreWindows( http://www.BkJia.com )
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');
$dbcolarray = array('id', 'name', 'age');
?>
<?php
// by MoreWindows( http://www.BkJia.com )
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');
$dbcolarray = array('id', 'name', 'age');
?>
2.smarty2.php文件
<?php
// by MoreWindows( http://www.BkJia.com )
header("Content-Type: text/html; charset=utf-8");
require('../../smart_libs/Smarty.class.php');
require_once('smarty2_head.php');
date_default_timezone_set("PRC");
//mysql_connect
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);
//表中記錄條數
$sql = sprintf("select count(*) from %s", DB_TABLENAME);
$result = mysql_query($sql, $conn);
if ($result)
{
$dbcount = mysql_fetch_row($result);
$tpl_db_count = $dbcount[0];
}
else
{
die("query failed");
}
//表頭
$tpl_db_coltitle = $dbcolarray;
//表中的內容
$tpl_db_rows = array();
$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);
$result = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))//與$row=mysql_fetch_assoc($result)等價
$tpl_db_rows[] = $row;
mysql_free_result($result);
mysql_close($conn);
$tpl = new Smarty;
$tpl->assign('db_count', $tpl_db_count);
$tpl->assign('db_coltitle', $tpl_db_coltitle);
$tpl->assign('db_rows', $tpl_db_rows);
$tpl->display('smarty2.html');
?>
<?php
// by MoreWindows( http://www.BkJia.com )
header("Content-Type: text/html; charset=utf-8");
require('../../smart_libs/Smarty.class.php');
require_once('smarty2_head.php');
date_default_timezone_set("PRC");
//mysql_connect
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);
//表中記錄條數
$sql = sprintf("select count(*) from %s", DB_TABLENAME);
$result = mysql_query($sql, $conn);
if ($result)
{
$dbcount = mysql_fetch_row($result);
$tpl_db_count = $dbcount[0];
}
else
{
die("query failed");
}
//表頭
$tpl_db_coltitle = $dbcolarray;
//表中的內容
$tpl_db_rows = array();
$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);
$result = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))//與$row=mysql_fetch_assoc($result)等價
$tpl_db_rows[] = $row;
mysql_free_result($result);
mysql_close($conn);
$tpl = new Smarty;
$tpl->assign('db_count', $tpl_db_count);
$tpl->assign('db_coltitle', $tpl_db_coltitle);
$tpl->assign('db_rows', $tpl_db_rows);
$tpl->display('smarty2.html');
?>
3.smarty2.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="smarty2.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="../jquery-1.7.min.js"></script>
<script type="text/javascript" src="smarty2.js"></script>
<title>{$smarty.const.DB_TABLENAME}</title>
</head>
<body>
<h1>表中有{$db_count}條記錄</h1>
<table border="1" align="center" cellpadding="10" cellspacing="2" bordercolor="#ffaaoo">
{foreach $db_coltitle as $col}
<th>{$col}</th>
{/foreach}
{foreach $db_rows as $dbrow}
<tr>
{foreach $dbrow as $k=>$val}
<td>{$val}</td>
{/foreach}
</tr>
{/foreach}
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="smarty2.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="../jquery-1.7.min.js"></script>
<script type="text/javascript" src="smarty2.js"></script>
<title>{$smarty.const.DB_TABLENAME}</title>
</head>
<body>
<h1>表中有{$db_count}條記錄</h1>
<table border="1" align="center" cellpadding="10" cellspacing="2" bordercolor="#ffaaoo">
{foreach $db_coltitle as $col}
<th>{$col}</th>
{/foreach}
{foreach $db_rows as $dbrow}
<tr>
{foreach $dbrow as $k=>$val}
<td>{$val}</td>
{/foreach}
</tr>
{/foreach}
</table>
</body>
</html>
4.smarty2.js文件
$(document).ready(function()
{
//用CSS控制奇偶行的顏色
$("table tr:odd").css("background-color", "#e6e6fa");
$("table tr:even").css("background-color", "#fff0fa");
});
$(document).ready(function()
{
//用CSS控制奇偶行的顏色
$("table tr:odd").css("background-color", "#e6e6fa");
$("table tr:even").css("background-color", "#fff0fa");
});
5.smarty2.css文件
@charset "utf-8";
h1
{
color:Red;
text-align:center;
}
table th
{
background-color:#7cfc00;
}
@charset "utf-8";
h1
{
color:Red;
text-align:center;
}
table th
{
background-color:#7cfc00;
}
程序運行結果如下:
上例展示了Smarty的基本用法,當然Smarty還提供了更加方便使用的接口函數,如對表格,可以使用{html_table}來快速生成表格。有興趣的讀者可以試試。
現在這個程序再加上《jquery 表格的增加刪除和修改及設置奇偶行顏色》中對表格的增加刪除和修改功能就基本上可以完善了,請參見下一篇《PHP訪問MySql數據庫 高級篇 AJAX技術》。
摘自 MoreWindows