ecshop首页增加商品评论
日期:2020-12-01
方法一:首先在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>
<!--评论结束-->
方法二:在库目录里增加一个文件comment.lbi里面有内容如下:
<?php
if (!function_exists("get_comments"))
{
function get_comments($num)
{
$sql = 'SELECT a.*,b.goods_id,b.goods_thumb,b.goods_name FROM '. $GLOBALS['ecs']->table('comment') .
' AS a,'. $GLOBALS['ecs']->table('goods') .'AS b WHERE a.status = 1 AND a.parent_id = 0 AND a.comment_type = 0 AND a.idvalue = b.goods_id '.
' ORDER BY a.add_time DESC';
if ($num > 0)
{
$sql .= ' LIMIT ' . $num;
}
//echo $sql;
$res = $GLOBALS['db']->getAll($sql);
$comments = array();
foreach ($res AS $idx => $row)
{
$comments[$idx]['add_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['add_time']);
$comments[$idx]['content'] = $row['content'];
$comments[$idx]['id_value'] = $row['id_value'];
$comments[$idx]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$comments[$idx]['goods_name'] = $row['goods_name'];
}
return $comments;
}
}
?>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!--数据调用-最新评论开始 -->
<?php
$this->assign('my_comments', get_comments(6)); // 数据条数
?>
<!--{foreach from=$my_comments item=comments}-->
<table width="546" border="0" cellspacing="0" cellpadding="0" style="border-bottom:1px #ccc dashed">
<tr>
<td width="73" height="70" align="center"><a href="goods.php?id={$comments.id_value}" target="_blank"><img src="{$comments.goods_thumb}" border="0" width="60" height="60" style="border:1px #CCCCCC solid"/></a></td>
<td width="473" align="left" valign="top">
<div style="height:3px;overflow:hidden"> </div>
<div><a href="goods.php?id={$comments.id_value}" target="_blank"><font style="color:#FF0000;font-size:14px">{$comments.goods_name}</font></a></div>
<div><img src="../images/pl.gif" width="38" height="19" align="absmiddle">{$comments.content|truncate:100:""}</div>
<div style="color:#999999">时间:{$comments.add_time}</div>
</td>
</tr>
</table>
<!--{/foreach}-->
首页调用这个库文件就ok了
<!-- #BeginLibraryItem "/library/comment.lbi" --><!-- #EndLibraryItem -->
本文原创地址:https://ecshopok.com/article-689.html
版权所有 © 转载时必须以链接形式注明出处!
觉得本文对您有用,想收藏下来!方法很简单:请点击-〉
我们一直坚持白天工作、晚上熬夜更新资源,付出了巨大的精力和时间,其中的辛酸难以言述。





暂无评论,来发表一个吧