[英]Sort comments by votes?
I'm making an application that is similar to stackoverflow in that it has ratings for questions and answers and I also have tabs that show comments by oldest, newest and votes. 我正在创建一个类似于stackoverflow的应用程序,因为它有问题和答案的评级,我也有标签显示最老,最新和投票的评论。 I'm having trouble sorting by votes. 我在按投票排序时遇到麻烦。
Here is my function: 这是我的功能:
/**
*
* @param int $threadid
* @param string $tab
* @param object $voting referencing Voting.class.php (may not be needed)
* @return database query/array
*/
public function getComments($threadid, $tab = 'oldest', $voting = null) {
if ($tab == 'oldest') {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date ASC";
} else if ($tab == 'newest') {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date DESC";
} else if ($tab == 'votes') {
//i dont know what to do here? read below for more explanation
} else {
$sql = "SELECT * FROM comments WHERE threadid = :threadid ORDER BY date ASC";
}
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':threadid', $threadid);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
Here is my database design: 这是我的数据库设计:
**comments:** | id | userid | threadid | message | date |
**commentsrating:** | userid | commentid | voteup | votedown |
If ratings are in a separate table from comments is it possible to make a query for $tab == 'votes'
that will conform with the rest of the code? 如果评级与评论分开,则可以查询符合其余代码的$tab == 'votes'
吗?
and finally HTML: 最后是HTML:
<?php //Get comments
if (isset($_GET['tab'])) {
$getComments = $thread->getComments($threadid, $_GET['tab'], $voting);
} else {
$getComments = $thread->getComments($threadid);
} ?>
<?php for ($i = 0; $i < count($getComments); $i++) { ?>
<p>
<?php echo $getComments[$i]['message']; ?>
</p>
<p>
<span class="bid_votes_count" id="bid_votes_count<?php echo $getComments[$i]['id'] ?>">
<?php echo $voting->getEffectiveCommentVotes($getComments[$i]['id']) . " votes"; ?>
</span>
<span class="bid_vote_buttons" id="bid_vote_buttons<?php echo $getComments[$i]['id'] ?>">
<a href="javascript:;" class="bid_vote_up" id="<?php echo $getComments[$i]['id'] ?>"></a>
<a href="javascript:;" class="bid_vote_down" id="<?php echo $getComments[$i]['id'] ?>"></a>
</span>
</p>
<?php } ?>
Thanks in advance!
Add rating
field to the comments
table and maintain it manually or with trigger on commentsrating
table. 将rating
字段添加到comments
表并手动维护或在commentsrating
表上使用触发器。
Obviously, put there pre-calculated value of the rating and now you are able to have a simple and terrible fast question to perform. 显然,将预先计算的评级值放在那里,现在你可以有一个简单而可怕的快速问题来执行。
Give this a shot: 试一试:
$sql = "SELECT * FROM comments c JOIN (SELECT commentid, ".
"(SUM(voteup) - SUM(votedown)) votes, " .
"FROM commentsrating GROUP BY commentid) i ON " .
"c.id = i.commentid WHERE c.threadid = :threadid " .
"ORDER BY i.votes DESC";
Obviously not the best performance with the crazy JOIN, but should get you to "it works," after which you can worry about performance. 显然不是疯狂JOIN的最佳表现,但是应该让你“有效”,之后你可以担心性能。
The fastest way would be storing a running tally of the votes as a field on the comments table (as mentioned in another answer). 最快的方法是将选票的运行记录存储为评论表中的字段(如另一个答案中所述)。 However you should also consider modifying the commentsrating table to make a little more sense and also help with the problem of tallying. 但是,您还应该考虑修改注释表以使其更有意义,并且还可以帮助解决计算问题。
commentsrating: | 评论: | userid | userid | commentid | commentid | vote | 投票|
The reason you should only have vote is because a comment rating can only have a single vote. 你应该只投票的原因是评论评级只能进行一次投票。 It can't be both an up AND down vote. 它不能同时上涨和下跌。
vote should be an int(1) +/-. 投票应该是int(1)+/-。 eg. 例如。 Can be 1 or -1 (or 0 theoretically). 可以是1或-1(理论上为0)。 This way you can do the following SQL: 这样您就可以执行以下SQL:
SELECT c.id, c.message, c.date, SUM(cr.vote) AS 'votes'
FROM `comments` c
JOIN `commentsrating` cr ON c.id=cr.commentid
WHERE c.threadid=':threadid'
GROUP BY c.id
ORDER BY `votes`
The query will take longer than an order by
total_votes field but this should be done anyway. 查询将比total_votes字段的order by
花费更长的时间order by
但无论如何都应该这样做。
SELECT * FROM `comments` WHERE `threadid`=:threadid ORDER BY (SELECT `voteup`-`votedown` FROM `commentsrating` WHERE `commentid`=`comments`.`id`) DESC
试试吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.