【发布时间】:2014-02-24 08:58:04
【问题描述】:
我试图显示每条评论有多少“回复”。
查询:
$sql = "SELECT c.*, a.username, a.avatar FROM user_wall c LEFT JOIN account a ON c.fid=a.id WHERE c.tid='". (int) $user[id]."' AND c.parent IS NULL ORDER BY `cid` DESC LIMIT 0, 10";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
{
$comments = array();
while ($row = mysql_fetch_assoc($result))
{
$cid = $row[cid];
$comments[] = $row;
$smarty->assign('comments', $comments);
// Get how many replies the comment has
$sql = mysql_query("SELECT count(*) AS `total` FROM `user_wall` WHERE parent='".$cid."'");
$tmp = mysql_fetch_assoc($sql);
$smarty->assign('rcount', $tmp['total']);
}
}
我在 smarty 模板上有什么:
{foreach item="comments" from="$comments"}
<div class="bubble2">
<div class="clearfix">
<div class="cinfo">By <a href="#">{$comments.username}</a>, {$comments.time}</div>
<div style="clear: both;"></div>
{$comments.comment}
<div class="c-actions">
<a href="#"><i class="icon-comments"></i> ({$rcount}) Replies</a>
</div>
</div>
</div>
{/foreach}
user_wall 表:
结果:
问题是现在每个评论都说它有“3”个回复,而唯一的回复是第二条评论(你好,这是一个测试!:-))。我怎样才能解决这个问题?有没有更好的方法来实现我的想法?
提前致谢。
【问题讨论】:
-
如果你像 if($tmp['total'] > 0 ) { $smarty->assign('rcount', $tmp['total']);} else { $ smarty->assign('rcount', 0);}
-
没用,每个帖子上仍然显示 3。
-
我认为你需要将计数包装在评论数组中,以便 smarty 循环显示。检查来自怀旧的答案
-
看来我做错了。也感谢您对我的问题感兴趣。