smarty模板引擎,列表循环方法

说明:mysql_connect()方法连接mysql数据库

inc.php代码:

$con = @mysql_connect($dburl, $uname, $upass);
if (!$con) die('MYSQL.Connect.Error!');
mysql_query("set names 'utf8'");
mysql_select_db($dbname, $con);

 

list.php代码

include_once 'include/inc.php';
include 'include/smarty/Smarty.class.php';
$smarty = new Smarty();//实例化
$smarty -> template_dir = './templates/'.THEME.'/';//模板目录
$sql="select * from content";
$res=mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($res))
{
$data[] = $row;
}
$smarty -> assign('list', $data);//给变量赋值

$smarty -> display('list.html');//显示模板

 

list.html模板代码:

代码一:

{foreach item=item from=$list}
<br>
标题:<a target="_blank" href="detail.php?id={$item.id}">{$item.c_title}</a>
内容:{$item.c_content}<br>
{/foreach}

代码二:
{section loop=$list name=list}

{$list[list].c_title}
{$list[list].c_content}

{/section}