ECShop添加最新评论功能实现步骤
以下是具体的操作步骤与思路解析,掌握后读者可自行扩展相关功能。
第一步:
打开 includes/lib_goods.php 文件,在其中构建一个用于获取最新评论的自定义函数。
参考以下代码逻辑进行编写: /** * 获取最近评论 * * @return array */ function get_latest_comment($limit_num) { $sql = "SELECT c.content,c.add_time,g.goods_id,g.goods_name FROM ".$GLOBALS['ecs']->table('comment')." AS c LEFT JOIN ".$GLOBALS['ecs']->table('goods')." AS g on c.id_value=g.goods_id WHERE c.status=1 LIMIT ".$limit_num; $res = $GLOBALS['db']->getAll($sql); return $res; }
第二步:
编辑根目录下的 goods.php 文件,定位到如下代码行: $smarty->assign('goods_rank', get_goods_rank($goods_id)); // 商品的销售排名
在该行代码下方插入以下新代码:
参考代码如下: $smarty->assign('comments_latest', get_latest_comment(10)); //获取最新品论
第三步:
进入 library 目录,新建一个名为 comments_latest.lbi 的文件。
第四步:
在模板文件 goods.dwt 中引入上述创建的 lbi 文件,即可在商品详情页面成功调用并展示“最新评论”模块。
具体调用代码如下: