I have the following tables:
post (id, title, content) etc
author (id, username) etc
author_vote (post_id, author_id, value)
The value is tiny_int, which can be 1 or -1.
I want to count the number of positive and negative votes for each message:
$posts = sql_select($link, "SELECT post.*, author.username
FROM post, author
AND author.id = post.author_id");
Why is the following code not working?
array_walk($posts, function(&$post, $link){
$post['positive'] = sql_select($link, "SELECT SUM(value) FROM author_vote WHERE post_id = $post['id']
AND value = 1");
$post['negative'] = abs(sql_select($link, "SELECT SUM(value) FROM author_vote WHERE post_id = $post['id']
AND value = -1"));
});
I also tried the following: this leads to the fact that all the voices are the same for each message:
foreach ($posts as &$post)
{
$id = $post['id'];
$post['positive'] = (int)sql_select($link, "SELECT SUM(value) FROM author_vote WHERE post_id = $id
AND value = 1");
$post['negative'] = (int)abs(sql_select($link, "SELECT SUM(value) FROM author_vote WHERE post_id = $id
AND value = -1"));
}
Also, is there a way to do this without having to query the database several times for each message? How will something that constantly changes [like this] get (mem) cached?