Quote:
Originally Posted by Ted S
Having a query on every post of a thread is a terrible idea that would add huge load to servers.
|
Should be able to do it with one query per page view. Once you have all the postids for the posts that will be displayed on the page, then pull all votes for all posts at once and sort them out in php. (If you're interested in implementing it

)
Something along these lines. (Code untested, likely has typos)
PHP Code:
// an array of postids for all posts shown on this page of thread
$postids;
$votes = $db->query_read("
SELECT pv_vote.*, user.username
FROM " . TABLE_PREFIX . "pv_vote AS pv_vote
LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.userid = pv_vote.userid)
WHERE pv_vote.postid IN (" . implode(',', $postids) . ")
");
$postvotes = array();
while ($vote = $db->fetch_array($votes))
{
if (!isset($postvotes["$vote[postid]"]))
{
$postvotes["$vote[postid]"] = array(
'totalscore' => 0,
'votecount' => 0,
'votes' => array(),
);
}
$postvotes["$vote[postid]"]['totalscore'] += $vote['vote'];
++$postvotes["$vote[postid]"]['votecount'];
$postvotes["$vote[postid]"]['votes'][] = array(
'username' => $vote['username'],
'userid' => $vote['userid'],
'vote' => $vote['vote'],
);
}