CodeIgniter Forums
Need help with counting articles on comments and categories on homepage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Need help with counting articles on comments and categories on homepage (/thread-27618.html)

Pages: 1 2


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]DumpProgrammer[/eluser]
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
Code:
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();    
    $this->load->vars($data);
    $this->load->view('newheader');
    $this->load->view('newnav');
    $this->load->view('newfeatured');    
    $this->load->view('newcontent');
    $this->load->view('newvideo');
    $this->load->view('newrightside');
    $this->load->view('newwidget');
    $this->load->view('footer');

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



Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]Maglok[/eluser]
You cannot make a countComments in your model?

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


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]DumpProgrammer[/eluser]
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?


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

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

Code:
function countComments($postid) {
    $this->db->from('comments');
    $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.


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]DumpProgrammer[/eluser]
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
Code:
$postid
yet. I have tried to add
Code:
$data['countcomments'] = $this->MComments->countComments($postid);
but that does not work because I have not yet defined the variable $postid.


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]Maglok[/eluser]
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.


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]DumpProgrammer[/eluser]
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.


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

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


Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]DumpProgrammer[/eluser]
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
Code:
<?php
if (count($post)){    
    $z= 1;
    foreach ($post as $id => $posted){
      
       if($z%2) {
   $i = "fl";  
        }
    else{
        $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;";        
      
        $z++;
        
        if($z%2) {
            
        echo "<div class='hl-full'></div> <div style='clear:both;'></div>";
        }
    }        
}

?&gt;



Need help with counting articles on comments and categories on homepage - El Forum - 02-16-2010

[eluser]Maglok[/eluser]
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.