To be frank, providing the previous “Blogger” controller class with the capacity for displaying all of the comments that have been made on each blog entry is only a matter of adding to the class a simple method which performs that specific task. However, it’s probable that this concept will be easier to understand if you look at the modified signature of the controller class. It includes a brand new method, called “comments(),” tasked with showing all the comments posted for each blog entry. Here it is: class Blogger extends Controller{ function Blogger(){ // load controller parent parent::Controller(); // load database class and connect to MySQL $this->load->database(); // load pagination library $this->load->library('pagination'); // load helper $this->load->helper('url'); } // display all blog entries function blogs(){ $data['title']='Blog Entries Listing'; $data['result']=$this->db->get('blogs',3,$this->uri->segment(3)); // set pagination parameters $config['base_url']='http://127.0.0.1/codeigniter/index.php/blogger/blogs/'; $config['total_rows']=$this->db->count_all('blogs'); $config['per_page']='3'; $config['full_tag_open']='<div id="paginglinks">'; $config['full_tag_close']='</div>'; $this->pagination->initialize($config); // create pagination links $data['links']=$this->pagination->create_links(); // load 'blogger_view' view $this->load->view('blogs_view',$data); } // display all blog comments function comments(){ $data['title']='Blog Comments Listing'; $this->db->where('blog_id',$this->uri->segment(3)); $data['result']=$this->db->get('blogs_comments'); // load 'blogs_comment_view' view $this->load->view('blogs_comment_view',$data); } } As I mentioned before, the above “Blogger” controller class now includes another method, not surprisingly called “comments().” It is responsible for fetching, from the pertinent “blogs_comments” MySQL table, all of the comments that have been posted about a particular blog entry. In order to perform this task, the method executes a WHERE conditional clause against the table using the active record pattern. It's worth noticing, by the way, that there are no SQL statements coded explicitly here. And finally, once the corresponding database rows has been retrieved, then they’re embedded into a view file so they can be displayed at a later time. Obviously, if you analyze this process pretty closely, you may now be asking yourself the following question: how can users post their comments on one or more blog entries? Well, I‘m glad you asked! As I explained a few lines above, the “comments()” method inserts the data retrieved from the “blogs_comments” MySQL table into a view file called “blogs_comment_view.php,” right? This file will include a simple HTML form which let users submit several comments on different blog entries. Now, are you starting to realize how this method does its thing? I bet you are! However, to dissipate any possible doubts that you might have regarding how the “comments()” method works, in the section to come I’ll be listing the signature of the “blogscomments_view.php” file. You'll be able to study it in detail. In the meantime, save the modified version of the previous “Blogger” controller class to the Code Igniter /system/application/controllers/ folder as “blogger.php,” then click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|