Welcome Guest, Not a member yet? Register   Sign In
Need help with counting articles on comments and categories on homepage

I am trying to count the comments for the articles that are displayed on my home page. I do have a function alreday that displays the comments but it relies on the article id which on my home page has not yet been passed. I also need to get the article category so that a user can easily go to read similar stories. I am not sure what is going to be helpful but I will post my homepage controller and get comments controller.homepage
function index()
    $data['cats'] = $this->MCats->getTopCategories(); //we will replace soon!
    $data['title'] = "Shout-Africa";
    $data['main'] = 'public_home';
    $data['featured'] = $this->MPosts->getAllFeaturedPosts();
    $data['post'] = $this->MPosts->getAllActivePosts();
    $data['comms'] = $this->MComments->getLatestComments();    

get article comments model
function getComments($postid){
     $data = array();
     $Q = $this->db->get('comments');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
    return $data;

You cannot make a countComments in your model?

Using the $this->db->count_all_results();

I am assuming I can put a count($comments) in the controller but in order for it to work I would need to put an ID in the getComments function. How have you executed such a request?

Well going from your getComments you know the $postid. So make a new function in the model like about so:

function countComments($postid) {
    $this->db->where('post_id', $postid);

    return $this->db->count_all_results();

You effectively query the comments from a post and count them. Count is also a rather 'cheap' SQL function.

I have got the count and category working on the article view working because when you read the post you pass the id to the getcomments functions but its the homepage where I am struggling because I do not have the variable
yet. I have tried to add
$data['countcomments'] = $this->MComments->countComments($postid);
but that does not work because I have not yet defined the variable $postid.

Well you are displaying posts on the mainpage right? That means you are grabbing the post from your database, thus you'd also grab your post_id, which you can then feed to countComments.

Thanks Maglok. I was just being dump. I didnt know you could call a function inside a view so thats what I have done. I see what you meant thanks.

You can, but you could also just do it in the controller and pass the array or object to the view.

Whenever I have been trying to put the getcomments($postid) in controller I get an error because I have not yet declared the $postid variable. Here is how I have added the getcomments in the view look for the getcomments, $category and $totalcomments
if (count($post)){    
    $z= 1;
    foreach ($post as $id => $posted){
       if($z%2) {
   $i = "fl";  
        $i = "fr";        
       $image_properties = array(
          'src' => 'uploads/'.$posted['image'],
          'alt' => $posted['title'],
          'class' => 'thumbnail',
          'width' => '100',
          'height' => '57',
          'title' => $posted['title']);
              echo "<div class='post $i'>";                      
           echo "<div class='box-post-content'>";
           echo anchor('blog/post/'.$posted['id'],img($image_properties));
           echo anchor('blog/post/'.$posted['id'],"<h2>".$posted['title']."</h2>");
           echo "<p><em>".$posted['pubdate']."</em></p>";

            echo "<p>".word_limiter($posted['body'],30)."</p></div>";
            echo "<p><span class='continue'>";
            echo anchor('blog/post/'.$posted['id'],'Read full story')."</span></p>";
        $category = $this->MCats->getCategory($posted['id']);
        echo "<p class='posted'>Posted in ".anchor("blog/category/".$posted['id'],$category['name']);
        $countcomments = $this->MComments->getComments($posted['id']);
        $totalcomments = count($countcomments);
        echo "<span class='comments'>".anchor('blog/post/'.$posted['id'],$totalcomments.' Comments');
        echo "</span></p></div>&lt;!--/post--&gt;";        
        if($z%2) {
        echo "<div class='hl-full'></div> <div style='clear:both;'></div>";


So $post is the query of posts. Don't see why you can't do it in the controller. Hell if you want you could even grab the commentcount on each post you select from the model in the model and tack it on.

That said, I wouldnt worry to much about where you grab the data exactly, though keeping MVC in the back of your head is a good thing.

Theme © iAndrew 2016 - Forum software by © MyBB