Welcome Guest, Not a member yet? Register   Sign In
Adding number of comments to blog tutorial.
#1

[eluser]tirithen[/eluser]
I'm trying to learn to think in the terms of controller and viewer, so as an exercise I'm trying to add feautres to the blog as shown in the video tutorial. Now I'm trying to add the number of comments to each post like 'Comments (8)' to the app so I changed the anchor to:

Code:
<p>&lt;?=anchor('blog/comments/'.$row->id,'Comments ('.$query2->num_rows(where('entry_id', $this->uri->segment(3))).')');?&gt;</p>

I allso added a db connection to
Code:
$query2
. Now I get three problems, one is that it shows he total number of posts instead of the specific without a where command, two is that I wrote the where command wrong and the third is that I now uses the view file as a controller.
What would be a good solution to this? Could I write a function that I run before the blog_view controller that stores the numer of comments per each posts in an array, could end up being a large array. Should I add a table on the sql server that keeps the count.

Any thoughts is welcome, this is what my blog_view file looks like now (non working anchor)

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>



&lt;?php foreach($query1->result() as $row): ?&gt;

<h3>&lt;?=$row->title?&gt;</h3>
<p>&lt;?=$row->body?&gt;</p>

<p>&lt;?=anchor('blog/comments/'.$row->id,'Comments ('.$query2->num_rows(where('entry_id', $this->uri->segment(3))).')');?&gt;</p>
<hr>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]ELRafael[/eluser]
what you can do

controller file
Code:
$comments = $this->db->getwhere('comments', array('post_id' => $this->uri->segment(3)));
$comments->num_rows()

Where:
- comments your table
- post_id the id of post that comment below
- $this->uri->segment(3) the id of post that you show in foreach loop

Get it?




Theme © iAndrew 2016 - Forum software by © MyBB