CodeIgniter Forums
help with blog model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: help with blog model (/showthread.php?tid=11043)



help with blog model - El Forum - 08-23-2008

[eluser]Richko[/eluser]
hi i am new to ci, so i am trying to code blog.

i have blog model

function get_entries() {
$this->db->orderby('id', 'DESC');
$query = $this->db->get('blog_entries');
foreach ($query->result_array() as $row)
{
$result[] = $row;
}
return $result;
}

i want to get comments count for each entry and send to view, but i dont realise how to do this...


help with blog model - El Forum - 08-23-2008

[eluser]stuffradio[/eluser]
If you just want to know how many comments it has.. you don't need all that.

First tell me how your table is laid out, and how you're storing your comments.


help with blog model - El Forum - 08-23-2008

[eluser]ehicks727[/eluser]
I'd have a separate function in your model just to get the comment count.

Code:
function getCommentCount($article_id) {
        $query = $this->db->query("SELECT COUNT(id) AS count FROM database.table WHERE article_id = ".$article_id.";");
        return $query->row();
    }

or something like that...


help with blog model - El Forum - 08-23-2008

[eluser]ehicks727[/eluser]
Then in your view, do something like this...

Code:
<?=$this->modelname->getCommentCount($article_id)->count ?>

I'm doing that off the top of my head, so someone please correct if I hosed it up.


help with blog model - El Forum - 08-23-2008

[eluser]Richko[/eluser]
thanks it worked for meSmile