Welcome Guest, Not a member yet? Register   Sign In
Having trouble getting num rows
#1

[eluser]kaos78414[/eluser]
I'm trying to get a simple comment count for the front page to show the count next to the read more, but I'm having a bit of trouble.

The code in my controller pulling the blogposts looks like this:
Code:
function index() {
        // $this->output->enable_profiler(TRUE);
        
        $data['js'] = '[removed][removed]';
        $data['post'] = $this->blogmodel->get_limited($limit = 5);
        $data['body'] = 'blogview';
        
        $data['count'] = $this->commentmodel->get_comment_count($blog_id);
        
        if ($data['count'] == 1) {
            $data['comments'] = 'Comment';
        }
        else {
            $data['comments'] = 'Comments';
        }
        
        $this->load->view('includes/template', $data);
    }

The get_comment_count function is a simple return num rows in my commentmodel:
Code:
function get_comment_count($blog_id) {
        $this->db->where('blogid', $blog_id);
        $q = $this->db->get('blogcomments');
        
        return $q->num_rows();
    }

Codeigniter returns this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: blog_id

Filename: controllers/blog.php

Line Number: 31


The thing is, I can't think of a way to define it correctly. I've tried a few things but with undesirable results. Any help is appreciated!


(Btw, I'd like to take a moment here to comment on how amazing the codeigniter community has been with me. I ask a lot of questions and I have never gotten a rude response. People always seem eager to help and I love that! Thanks guys!)
#2

[eluser]mddd[/eluser]
The get_comment_count part is not te problem. The 'undefined' error is occurring in your controller. That's even before your get_comment_count method is called!
It's simple to see why:
Code:
$data['count'] = $this->commentmodel->get_comment_count($blog_id);
In this code, you use $blog_id but where does that come from?? It is not defined anywhere in the lines above!
How does the controller know which $blog_id it is looking for??

I think if you correct that, you will find that the count works fine.
#3

[eluser]kaos78414[/eluser]
Yes, I'm sorry the original post was poorly typed out.

The problem is that however I try to define $blog_id, I get an error. I'm trying to figure out an efficient way to get the blog id.
#4

[eluser]mddd[/eluser]
Then tell us what the url of your page looks like. The blog_id should be in there somewhere..
The site can't guess on its own which blog you mean, right Smile
#5

[eluser]kaos78414[/eluser]
Well this is the home page (it's displaying multiple truncated entries). So the best way to get individual blog id's is with something like $row->blogid, the get limited function looks like this:

Code:
function get_limited($limit) {
        $this->db->order_by("blogid", "desc");
        $q = $this->db->get('blogposts', $limit);
        return $q->result();
    }
#6

[eluser]kaos78414[/eluser]
But if I do something like $blog_id = $data['post']->blogid, I get no result.
#7

[eluser]mddd[/eluser]
It feels like your mixing up a few things. Let me get it straight;
* first you want to get a few recent posts
* then you want to see how many comments each post has.

In your code, you are first getting a LIST of posts, and then acting if you got only one. $data['post'] is not ONE post, but 5! So if you wanted to get the blogid of those posts, you would have to use a foreach loop to check each of them..

But really, this is something you best do in one go: use a Mysql query with a JOIN to get the recent posts, along with their comment counts.
Something like
Code:
$this->db->select('blogposts.*, COUNT(blogcomments.id) comments', false);
$this->db->from('blogposts');
$this->db->join('blogcomments', 'blogcomments.blogid = blogposts.blogid', 'left');
$this->db->order_by('blogposts.blogid', 'desc');
$this->db->limit(5);
$result = $this->db->get()->result;
The 'comments' column in the result should contain the number of comments found for each post.
If this seems hard, check the Mysql manual on JOIN.
#8

[eluser]kaos78414[/eluser]
Sorry for the multiple posts, but to answer your question about the url, it contains no segment information since it is the index page. I can't get the blog id that way in this specific instance. But I know there's a way to do this, it's barely eluding me

EDIT: oh okay I'll give that a shot.
#9

[eluser]kaos78414[/eluser]
I'm not sure I understand how that query is counting the comments per each different blog. Was that supposed to be a standalone solution or..? I'm not sure I understood correctly but now I'm only getting 1 blog post and showing an incorrect comment count:

This is the model now:
Code:
function get_limited($limit) {
        $this->db->select('blogposts.*, COUNT(blogcomments.blogid) comments', false);
        $this->db->from('blogposts');
        $this->db->join('blogcomments', 'blogcomments.blogid = blogposts.blogid', 'left');
        $this->db->order_by('blogposts.blogid', 'desc');
        $this->db->limit($limit);
        $result = $this->db->get()->result();
        
        return $result;
}

And the controller:
Code:
function index() {
        $data['post'] = $this->blogmodel->get_limited($limit = 5);
        $data['body'] = 'blogview';

        $this->load->view('includes/template', $data);
}
#10

[eluser]mddd[/eluser]
I'm sorry. I was typing a bit quickly.
A few corrections:
Code:
// don't count blogid, because that's always going to be 1. it's the id's of the comments we need to count
// so replace the "select" line:
$this->db->select('blogposts.*, COUNT(blogcomments.id) comments', false);

// group the result by blogpost, otherwise we still always get a number of 0 or 1
// so add a "group_by" line:
$this->db->group_by('blogposts.blog_id');




Theme © iAndrew 2016 - Forum software by © MyBB