首先:在index.php文件中定义取得评论的函数
/**
* 获得最新的评论列表。
*
* @access private
* @return array
*/
function get_mycomments($num)
{
@$sql = 'SELECT * FROM ecs_comment '.
' WHERE status = 1 AND parent_id = 0 AND comment_type=0 AND comment_rank!=0'.
' ORDER BY add_time DESC';
if ($num > 0)
{
$sql .= ' LIMIT ' . $num;
}
$res = $GLOBALS['db']->getAll($sql);
$comments = array();
foreach ($res AS $idx => $row)
{
$comments[$idx]['user_name'] = $row['user_name'];
$comments[$idx]['content'] = $row['content'];
$comments[$idx]['id_value'] = $row['id_value'];
}
return $comments;
}
以及定义给模板$smarty->assign('my_comments', get_mycomments(5)); // ‘5’代表首页显示5条评论
在首页index.dwt中调用显示:
<!--用户评论开始-->
<ul>
<!--{foreach from=$my_comments item=comments}-->
<li><a href="goods.php?id={$comments.id_value}">{$comments.content|truncate:10:""}</a></li>
<!--{/foreach}-->
</ul>
<!--评论结束-->