As I expressed in the section that you just read, users will be able to submit several comments on a particular entry by way of a simple web form. As you may guess, this web page element needs to be coded into a view file, since it's a part of the blogger's visual presentation. In order to insert these comments into the "blogs_comments" MySQL table, it's necessary to define another method within the controller class that performs this task in a straightforward manner. Below I listed the modified version of the controller in question. It incorporates a brand new method called "insert_comment()" that comes in useful for storing user-submitted posts on the corresponding MySQL table. Take a look at it, please: 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 'blogs_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); } // insert new blog comment function insert_comment(){ $this->db->insert('blogs_comments',$_POST); redirect('blogger/comments/'.$_POST['blog_id']); } } Despite its short signature, the above "insert_comment()" method is indeed quite useful. It inserts a new comment into the "blogs_comments" MySQL table, and then redirects the user to the comments web page when this insertion operation has been accomplished correctly. As you can see, comments are collected via a POST HTML form, so in this case the $_POST PHP superglobal array is used for submitting this data to the respective database table. Okay, at this stage the "Blogger" controller class has the ability to store new comments about different blog entries on a specific MySQL table. Nevertheless, this process would be rather incomplete if I didn't show you how to code the web form that lets users to accomplish this task in a simple fashion. Therefore, in the last section of this article I'll be teaching you how to redefine the signature of the "blogs_comment_view.php" file to include the mentioned online form. Please, jump forward and read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|