![]() |
noob: trying to build on "blog tutorial" - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: noob: trying to build on "blog tutorial" (/showthread.php?tid=8693) Pages:
1
2
|
noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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> blog.php Code: <?php noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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. noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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"; Thanks, Jamie noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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). noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [eluser]Jamie Rumbelow[/eluser] well, you've got it in one. Just do this: blog_view.php Code: <?php $this->load->helper("comment"); ?> Then create a new helper. helpers/comment_helper.php Code: function get_comments_no($id) That should do the trick, and it introduces you to new helpers! Thanks, Jamie noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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 noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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. noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [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: <html> helpers/comment_helper.php Code: function get_comments_no($id) noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [eluser]Jamie Rumbelow[/eluser] Not really sure what's going wrong. Try reading the user guide page on Helpers. noob: trying to build on "blog tutorial" - El Forum - 05-27-2008 [eluser]cirox![/eluser] I will do. Thanks for you help and your patience. Hope it's going to be an easy one.. |