Welcome Guest, Not a member yet? Register   Sign In
Simple Question: Expanding on the Blog Tutorial
#1

[eluser]MT206[/eluser]
Hi, I am new to CI and I am having trouble wrapping my head around some simple things. Right now all I am trying to do is just put the number of comments a blog post has under the post but for the life of me I cant figure out how to. I understand how you would do it with just PHP but obviously the point is to have CI make life easier so I am trying to use it. Any help would be much appreciated. Thanks

For people not familiar with the blog video tutorial here is the controller code:

Code:
function Blog(){
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('form');
    }
    function index(){
        $data['title'] = 'Mike\'s Blog';
        $data['heading'] = 'Mike\'s Blog Heading';
        $data['query'] = $this->db->get('entries');
        $this->load->view('blog_view', $data);
    }
    function comments(){
        $data['title'] = 'Mike\'s Blog';
        $data['heading'] = 'Mike\'s Comment Heading';
        $this->db->where('entry_id', $this->uri->segment(3));
        $data['query'] = $this->db->get('comments');
        
        
        $this->load->view('comment_view', $data);
    }
    function comment_insert(){
        $this->db->insert('comments', $_POST);
        redirect('blog/comments/' . $_POST['entry_id']);
    }
}
?>


and the blog view code:
Code:
<html>
    <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
    <h1>&lt;?php echo $heading; ?&gt;</h1>
        &lt;?php foreach($query->result() as $row): ?&gt;
            <h3>&lt;?php echo $row->title; ?&gt;</h3>
            <p>&lt;?php echo $row->body; ?&gt;</p>
            
            <p>&lt;?php echo anchor('blog/comments/' . $row->id, 'Comments'); ?&gt;</p>
            <hr>
        &lt;?php endforeach; ?&gt;
    &lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Bainzy[/eluser]
Hi,

the simple way in which to show the number of comments is to modify the controller slightly, all you need to do is in the controller change the line that reads

Code:
$data['query'] = $this->db->get('comments');

to

Code:
$data['comments'] = $this->db->get('comments');

then on your view file have the following or something similar

Code:
&lt;?php echo '<cite>'.count($comments).'</cite>'; ?&gt;

the simple count statement will count how many results are in the comments table.

Hope this helps
#3

[eluser]MT206[/eluser]
Thanks, I think I was just over thinking this.
#4

[eluser]MT206[/eluser]
I have still not been able to get this to display the proper count. I might be misunderstanding what was shown in the above post but I do not think so.
From the code examples given above I do not understand why I would be changing the 'query' to 'comments' as that seems arbitrary and when I do I get the error: undefined variable comments which makes sense since there is no 'comments' variable in the index function to pass to the view. For more information the comment view looks like this:

Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1>&lt;?php echo $heading; ?&gt;</h1>
        
        &lt;?php if ($query->num_rows() > 0): ?&gt;
            &lt;?php foreach($query->result() as $row): ?&gt;
                <p>&lt;?php echo $row->author; ?&gt;</p>
                <p>&lt;?php echo $row->body; ?&gt;</p>            
                <hr>
            &lt;?php endforeach; ?&gt;
        &lt;?php endif; ?&gt;
        
        &lt;?php echo form_open('blog/comment_insert'); ?&gt;
        &lt;?php echo form_hidden('entry_id', $this->uri->segment(3)); ?&gt;
            Comment:
            <p>&lt;textarea name="body" rows="10"&gt;&lt;/textarea></p>
            Author:
            <p>&lt;input type="text" name="author" /&gt;&lt;/p>
            <p>&lt;input type="submit" value="Submit Comment" /&gt;&lt;/p>
        &lt;/form&gt;
        
        <p>&lt;?php echo anchor('blog', 'Back to Blog'); ?&gt;</p>
    &lt;/body&gt;
&lt;/html&gt;

And the Database setup is:

entries
id, title, body
comments
id, entry_id, author, body


Would I need another table that contains both entry_id and comment_id? I do not see how I could use data from both tables without manually writing an SQL query involving a JOIN or actually having a column for comment count which seems clunky.
#5

[eluser]atno[/eluser]
Try this code
Code:
function total_comments()
    {
        $query = $this->db->get_where('comments',array('entry_id' => $this->uri->segment(3)));
        echo $query->num_rows();
    }
Will return number of rows fetched from table comments for the entry your are trying to display.

Edit: i don't have the time to create the blog tutorial again so i haven't test this code




Theme © iAndrew 2016 - Forum software by © MyBB