php文件
<?php
mysql_connect("localhost","","");
mysql_query("set names 'gb2312'");
mysql_select_db("shop");
$sqlstr="select * from goods";
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/templates/templates";//在项目根目录下面建此文件夹
$smarty->compile_dir ="smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir ="smarty/templates/cache";
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;
$exec=mysql_query($sqlstr);
$rs=mysql_fetch_object($exec);
$arr=array();
$thenumber=0;
while($rs)
{
$arr2=array();
$arr2[0]=$rs->goodsname;
$arr2[1]=$rs->price;
$arr[$thenumber]=$arr2;
$rs=mysql_fetch_object($exec);
$thenumber=$thenumber+1;
}
$smarty->assign("guests",$arr); //$arr为二维数组,将它赋给模板中的$guest变量
$smarty->display('index.tpl');
?>
tpl文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
<title>Smarty</title></head>
<body>
<table border="1">
{foreach from = $guests item=guest} //item为选项取名
<tr>
<td>{$guest.0}</td> //$guest与item的值相同,0与$arr2[0]相同。
<td>{$guest.1}</td>
</tr>
{/foreach}
</table>
</body>
</html>