Welcome Guest, Not a member yet? Register   Sign In
noob: trying to build on "blog tutorial"
#1

[eluser]cirox![/eluser]
Hi everyone,

I'm new to both ci & php and I need your help. I've successfully followed the online video - blog system tutorial - and I'm now attempting to add some more functionality to it as an exercise.

Namely, I'm trying to get the "Comments" link to show the number of comments, e.g. "Comments(8)".

I can't think of an easy way to do it, without violate (what's my understanding of) MVC.

I'd be grateful if someone could tell me a straightforward of doing it.

Thanks.

blog_view.php
Code:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>&lt;?php echo $title; ?&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;

blog.php
Code:
&lt;?php

class Blog extends Controller {

    function Blog()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
    }

    function index()
    {
        $data['title'] = "My Blog Title";
        $data['heading'] = "My Blog Heading";
        $data['query'] = $this->db->get('entries');
        
        $this->load->view('blog_view', $data);
    }
    
    function comments()
    {
        $data['title'] = "My Comment Title";
        $data['heading'] = "My Coment 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']);
    }
}

?&gt;
#2

[eluser]AgentPhoenix[/eluser]
Actually, CI has a pretty shiny object associated with the Database library that'll return the number of rows a query generates. Once you've run your query, you could do something like this (assuming you've assigned the query result to a variable called query):

Code:
$data['num_comments'] = $query->num_rows();

Then in your view, you can just echo out $num_comments wherever you want that count.
#3

[eluser]Jamie Rumbelow[/eluser]
Hello cirox!, and welcome to the CI forums!

Yes, as AgentPheonix said, the num_rows() function is a great function to count the number of rows associated with the query that you ran. I prefer to use multi-dimensional arrays when I require sending lots of data from the controller to views.

So in this case I would do something like this:

Code:
$data['blog_info']['name'] = "My Blog";
...
...

$data['comments']['number'] = $query->num_rows();
...
...

Thanks,

Jamie
#4

[eluser]cirox![/eluser]
I'm still not getting it.

$query is my original query that returns the blog entries. (table `entries`)

My understanding is that I need to run a separate query that will return the number of comments for a particular $entry_id. (different table `comments`)

But because the posts are extracted in the foreach loop inside the view, that's the only place I can see fit to run the query. (i.e. that's where I will be able to pass the entryid of the current $row as a parameter to the second query).
#5

[eluser]Jamie Rumbelow[/eluser]
well, you've got it in one. Just do this:

blog_view.php
Code:
&lt;?php $this->load->helper("comment"); ?&gt;
&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 ('.get_comments_no($row->id).')'); ?&gt;</p>


<hr />
&lt;?php endforeach; ?&gt;

Then create a new helper.

helpers/comment_helper.php
Code:
function get_comments_no($id)
{
$q = $this->db->get("comments");
return $q->num_rows();
}

That should do the trick, and it introduces you to new helpers!

Thanks,

Jamie
#6

[eluser]cirox![/eluser]
That's exactly what I did the first time. But I was getting:

Fatal error: Call to undefined function get_comments_no()

I will try again now and let you know! Thanks.

EDIT: No, actually I defined that function inside the controller, not in a new file. Do you know what the difference is, and why it was throwing that error? Thanks
#7

[eluser]Jamie Rumbelow[/eluser]
Ah!

Controller functions are only accessible from the URL (/blog/posts). Helpers give you access to function that can be used anywhere in your application.
#8

[eluser]cirox![/eluser]
now when I refresh I get the actual function printed out on the page?

-----------------------------------------------------------------------------------------
My Blog Title

function get_comments_no($id) { $q = $this->db->get("comments"); return $q->num_rows(); }

entry title


dummy txt entry.


Fatal error: Call to undefined function get_comments_no() in /var/www/CodeIgniter_1.6.2/system/application/views/blog_view.php on line 17
-----------------------------------------------------------------------------------------

blog_view.php
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 $title; ?&gt;</h1>

&lt;?php $this->load->helper("comment"); ?&gt;

&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 ('.get_comments_no($row->id).')'); ?&gt;</p>


<hr />
&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;


helpers/comment_helper.php
Code:
function get_comments_no($id)
{
$q = $this->db->get("comments");
return $q->num_rows();
}
#9

[eluser]Jamie Rumbelow[/eluser]
Not really sure what's going wrong. Try reading the user guide page on Helpers.
#10

[eluser]cirox![/eluser]
I will do. Thanks for you help and your patience. Hope it's going to be an easy one..




Theme © iAndrew 2016 - Forum software by © MyBB