Welcome Guest, Not a member yet? Register   Sign In
Counting comments for blogitem
#1

[eluser]Rein Van Oyen[/eluser]
Hi there CI-community,


I'm new to CodeIgniter and I absolutely love it. Altough, I bumped against a first problem. (Oh well...or lack of knowledge on my end). I'm making a simple blog and I want to count the comments for each blogitem. I have three tables. One for the blogentries, one for the categories and one for the comments. Now what I have is a simple model which returns the blogentries.
Code:
function get_entries($limit='') /* gets a list of blogitems, limit can be specified */
    {
    
        if(empty($limit)){
            $this->db->select('*')->from('blog_entries')->join('blog_categories', 'blog_categories.category_id = blog_entries.entry_parent_id')->order_by('entry_id','desc');
            $query = $this->db->get();
        }
        else
        {
            $this->db->select('*')->from('blog_entries')->join('blog_categories', 'blog_categories.category_id = blog_entries.entry_parent_id')->order_by('entry_id','desc')->limit($limit);
            $query = $this->db->get();
        }
        
        return $query;
    }

I also have this simple code in my commentmodel which returns the comments for the detail page of the blogposts:
Code:
function get_comments($parent, $parent_id)
    {
        $query = $this->db->get_where('comments', array('comment_parent_id' => $parent_id, 'comment_parent' => $parent));            
        return $query;
    }

Now for the blog-listing I want to show the amount of comments for each blogpost. What is the best way to accomplish this? I can't seem to find out how to get the commentscount inside of my loop (in the view).

Thanks in advice,
Rein
#2

[eluser]LuckyFella73[/eluser]
Are you looking for this ?
Code:
$this->db->where('comment_parent_id', $parent_id);
$this->db->from('comments');
$this->db->count_all_results(); // number of comments related to one blockentry
#3

[eluser]Rein Van Oyen[/eluser]
First of all; thanks for your fast reply!

It is not really what I meant. I'll make myself clear. Smile

I loop through the results of the first code I posted (get_entries). I loop through it in my view.

Code:
<?php foreach($blogentries as $blog){ ?>
<h3>&lt;?=$blog->entry_title?&gt;"></h3>
<p>
&lt;?=$blog->entry_body?&gt;
</p>
&lt;?php } ?&gt;

Now I want to display the comment-count in this loop like you see on many blogs. (eg. comments (12) - Read more)
I can't seem to find a way to implement this in my code. Is it because the code I wrote is complete garbage or is there a way out? Smile

Thanks again!
#4

[eluser]LuckyFella73[/eluser]
Ah ok - I thought it should be displayed under a single blog entry ..

What you can do is doing the loop in your model push the values
into an array and get the number of comments while looping

Basically like this:
Code:
function get_entries($limit='') /* gets a list of blogitems, limit can be specified */
{

    if(empty($limit)){
        $this->db->select('*')->from('blog_entries')->join('blog_categories', 'blog_categories.category_id = blog_entries.entry_parent_id')->order_by('entry_id','desc');
        $query = $this->db->get();
    }
    else
    {
        $this->db->select('*')->from('blog_entries')->join('blog_categories', 'blog_categories.category_id = blog_entries.entry_parent_id')->order_by('entry_id','desc')->limit($limit);
        $query = $this->db->get();
    }
    if ($query->num_rows() > 0)
    {
        $blog_array = array();
        foreach ($query->result_array() as $row)
        {
            # whatever fields you have
            $blog_array['date'][] = $row['date'];
            $blog_array['title'][] = $row['title'];
            $blog_array['bodytext'][] = $row['text'];
            $blog_array['number_of_comments'][] = $this->count_comments($id_blockentry); // provide the id of the blockentry the comments are related to here
        }
    }
    return $blog_array;
}


function count_comments($id_blockentry = FALSE)
{
    $this->db->where('comment_parent_id', $parent_id);
    $this->db->from('comments');
    return $this->db->count_all_results();
}

You will have to adjust some values of the code according to your db table columns,
the part: "$this->count_comments($id_blockentry);" and add some if statements in case
there are no comments and so on. But in generall you could do it like this.

In your view file loop through the array returned by the model (pass from controller
to the view of course)

Hope that helps ..



Edit: fixed a typo in model code: '_' was missing (number_of_comments)
#5

[eluser]Rein Van Oyen[/eluser]
LuckyFella73, that sure helps! It is exactly what I was looking for. I guess I know now that I can do all that in my model. I was kinda confused about what to do where and used my models only to run queries.

Thanks alot, this gave me more insight. Smile
#6

[eluser]Rein Van Oyen[/eluser]
Ok, I've come a long way with the code of LuckyFella. But I was just wondering what the best way is to loop through it in the view now. When to use objects instead of arrays? The arrays seem much easier to use, imo. What is the advantage of using objects?

Thanks!

(edit: sorry for doubleposting!)
#7

[eluser]Atharva[/eluser]
[quote author="Rein Van Oyen" date="1292505431"]Ok, I've come a long way with the code of LuckyFella. But I was just wondering what the best way is to loop through it in the view now. When to use objects instead of arrays? The arrays seem much easier to use, imo. What is the advantage of using objects?

Thanks!
[/quote]

There is not much difference between them, but since we are using an Object Oriented Framework , many developer prefer to use objects than array. I also prefer to use result() function which returns array of objects, rather than result_array() which returns a pure array.
#8

[eluser]Rein Van Oyen[/eluser]
Thanks for the reply Atharva, I guess the difference will become more clear while gaining experience! Smile

In the code LuckyFella73 proposed, how would the view look like? Do I need to use a foreach in a foreach to loop through that array?
#9

[eluser]Atharva[/eluser]
[quote author="Rein Van Oyen" date="1292517281"]

In the code LuckyFella73 proposed, how would the view look like? Do I need to use a foreach in a foreach to loop through that array?[/quote]

Nope
Code:
foreach ($query->result() as $row)
        {
            # whatever fields you have
            $blog_array['date'][] = $row->date;
            $blog_array['title'][] = $row->title;
            $blog_array['bodytext'][] = $row->text;
            $blog_array['number_of comments'][] = $this->count_comments($id_blockentry); // provide the id of the blockentry the comments are related to here
        }
#10

[eluser]LuckyFella73[/eluser]
My thought was to do it simular to this:
Code:
// controller:
$data['query_blog'] = $this->Your_model->get_entries(); // store DB values as array here
$this->load->view('your_viewfile', $data);


// view:
$count_entries = count($query_blog);
for ($i=0; $i<$count_entries; $i++)
{
    // indexes like you did set up in your model (here according to my last example):
    echo $query_blog['date'][$i].'<br />';
    echo $query_blog['title'][$i].'<br />';
    echo $query_blog['bodytext'][$i].'<br />';
    echo $query_blog['number_of_comments'][$i].'<br />';
}

So you have all data related to one blog entry (including the coment count)
in one array set.




Theme © iAndrew 2016 - Forum software by © MyBB