noob: trying to build on "blog tutorial" |
[eluser]cirox![/eluser]
Managed to get everything working. The reason why the load helper command was spitting out the helper function as text was because (to my embarassment) I hadn't enclosed the function in <?php ?> tags. Also, the final function is slightly different: a) It needed a where clause for it to return what I wanted. Duh! b) It was giving me Fatal error: Using $this when not in object context which I'm not sure what to make of. However, I tried to pass the object from the view as a parameter, like so: Code: get_comments_no($row->id, $this) to the comment_helper.php: Code: <?php and this worked fine. I also managed to get it to work without the helper, which was what I was initially trying to achieve. I had originally defined get_comments_no() inside the controller, but when I tried viewing the page it was throwing a Fatal error: Call to undefined function get_comments_no() ... The reason, I figured out, was because I'd defined the function outside the index() function, and that way, the view didn't know about it. I've got so much to learn! This is the final code [for other newbies' benefit] ;-P blog.php Code: <?php blog_view.php Code: <html>
[eluser]Jamie Rumbelow[/eluser]
Well done! Just one quick note: Defining a function inside another function, particularly one of a controller, is about as far from MVC coding standards as you could hope. Try to adhere; that's what helpers and libraries are for in the first place! Thanks, Jamie
[eluser]ajrowland[/eluser]
Hello. For efficiency you could use a left join on your query to obtain the number of comments for each entry. Then you would not need to run an extra query on each entry just to pull out the number of comments. For example change your index method to: Code: function index() Therefore your view would be something like: Code: <html> It may seem minor, but now your rendered page only requires one database query no matter how many entries, and therefore scales better.
[eluser]cirox![/eluser]
@andy that's a good tip, cheers mate! I will definitely use it when I next need to do something similar. I really like this syntax Code: $this->db->select
|
Welcome Guest, Not a member yet? Register Sign In |