问题描述:( V* K8 R$ @8 G5 K k# \5 O
我想让商品列表页(category.php)的每个商品的下面显示该商品的评论数量以及评论等级,该如何操作呢??+ \ f& V6 H/ U
( ^/ t; B* M; G! W3 X9 g
效果图示:
/ S. j( n8 w# u* s& B
& C0 Y( z; k# _7 F$ n# w& x
! _; Y8 q1 \4 q8 {- C3 s2 h! N
解决方法:, D" O9 r; [. E( X6 X# r% t
第一步:8 a2 y: X( M1 B$ A. m% q z3 Q$ ]
首先修改category.php 文件,定位到 category_get_goods() 函数部分0 P% `$ W, v# B
找到 $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .( r; @, h" z5 F5 j% W
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
) \& Z8 e: W0 f/ j) Q- V 'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .# ?8 f! n: ]: O2 J& M- V5 p
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .7 N7 i+ s% b! b' T y+ x3 f
'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
4 z/ p( b$ R. @ @: u "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .0 ?) w5 x% z. x+ f7 Z9 _& R
"WHERE $where $ext ORDER BY $sort $order"; | ( q$ B! P9 B4 y8 i: r- b4 Y
将之修改为$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .7 [+ J# O( V4 N3 ^' o+ Q4 D7 b% u
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
4 q+ M4 _ o" I. \0 I: q " IFNULL(AVG(r.comment_rank),0) AS comment_rank,IF(r.comment_rank,count(*),0) AS comment_count, ".2 E L3 t3 A3 Q5 N1 u/ s' k
'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
p2 q1 R( o- F+ U8 G$ R% x 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
4 g; {" n6 }+ s C% {0 v 'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
4 n" i. J/ W, a% ~1 x "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
s$ Q% d- e9 N3 f( b* B$ \, A" }7 d! b ' LEFT JOIN '. $GLOBALS['ecs']->table('comment') .' AS r '.- N3 ]* q3 d. c8 X9 C
'ON r.id_value = g.goods_id AND comment_type = 0 AND r.parent_id = 0 AND r.status = 1 ' .2 I' \) y2 N0 E# _$ b5 V( e/ U
"WHERE $where $ext group by g.goods_id ORDER BY $sort $order"; |
, K$ E5 B6 H' H& [( {! }$ \. T然后继续找到$arr[$row['goods_id']]['url'] = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']); | 7 k4 F; S. F1 ^: X. i% {. l; b
在它下面增加几行代码$row['comment_rank'] = ceil($row['comment_rank']) == 0 ? 5 : ceil($row['comment_rank']);4 G! ]" f; d% y
$arr[$row['goods_id']]['comment_rank']=$row['comment_rank'];
! }9 A. r) P9 E+ ?2 L$arr[$row['goods_id']]['comment_count']=$row['comment_count']; |
! m/ M' J* q/ Z+ H0 \3 Q0 x第二步(以官方默认模板为例): \/ Z# ]0 G# W2 u, Q* M, V; I
修改 htemes/default/library/goods_list.lbi 文件3 f) B" P; m2 ?: P/ g5 H7 D6 H
找到
, H0 [" i' s) Y/ ?9 c<a href="javascript:collect({$goods.goods_id});" class="f6">{$lang.btn_collect}</a> | 0 n' d3 g- Q P2 d
在它上面增加
7 x2 ^; D% g1 ~$ ?( @3 d<img src="images/stars{$goods.comment_rank}.gif" style="width:78px;height:15px;" /><br>
+ e, k0 t3 c+ m+ k% t8 s% h评论数:{$goods.comment_count}<br> |
+ y8 m) s: G* x8 ^4 c: ?结束语:
" m7 w# X" L. ?大家都知道ECSHOP系统有缓存机制,如果某个商品有了新的评论,列表页的评论数量不会立马随着更新。得等到下次更新缓存的时候才会更新。
2 B5 C0 V) [2 S8 B
$ W- }( ]+ L/ y2 v$ B8 [; f如果您想列表页能即时体现评论数量的变化。只需继续进行下面修改即可. b) T/ q4 K- F# O3 u
------------------------------------------------------------------------------------------------
. S7 i1 r: t" X( Q. ^# N# Z把 category.php 文件的 ( s% ]( l3 B$ n% ]
$smarty->caching = true;
V$ g* @; [2 p# a7 d修改为& G4 T3 S; Q) \* D- l
$smarty->caching = false;
7 m2 L+ v! G+ [ Q8 Z7 }6 A----------------------------------------------------------------- v% @) J& O" B! \9 k
|