I stumbled upon a logic problem today with Codeigniter. After 4 hours, I thought I should better ask here.
My project main page contains:
- Top posts (Attributes: Title, Logo, Excerpt)
- Top Commentaries for each Post (Attributes: Member-name, Avatar)
My problem is with getting the top commentaries for each post.
If I'm not using Code Igniter this is how I will do it:
<?php
foreach( $posts as $post ) {
echo 'Post Title: '.$post['title'].'<br/>';
$top_commenters = $this->get_top_commenters($post['id']);
foreach( $top_commenters as $commenter ) {
echo '<img src="'. $commenter['avatar'] .'"/><br/>';
}
}
?>
I think you noticed that I'm passing each Post's ID to a function, to get its top commenters. However, in CodeIgniter, I can't call a controller from a view.
One last solution I would use is Ajax. But I hope I get some help here.
Thanks in Advance!
UPDATE: as requested, here are my Model function and Controller function:
//Model Function
function get_popular_posts() {
$row = $this->db->query('
SELECT * FROM posts
INNER JOIN popular_posts ON posts.id = popular_posts.pid
ORDER BY rank DESC LIMIT 6
');
return $row->result_array();
}
//Model Function
function get_top_commenters($post_id){
$row = $this->db->query('
SELECT *
FROM users
WHERE username
IN (
SELECT username
FROM comments WHERE pid = '. $post_id .')
ORDER BY likes DESC LIMIT 6
');
return $row->result_array();
}