Home arrow PHP arrow Page 3 - Using Code Igniter to Enable Comments in a Blog Application

Inserting comments with a Blogger controller class method - PHP

Welcome to the fourth episode of the series entitled “Building a Blogger with the Code Igniter PHP framework.” Comprised of seven approachable articles, this series steps you through the development of a blog application using this friendly – yet powerful -- PHP framework. We include copious code samples to demonstrate how to use the MVC pattern to develop a real-world program.

TABLE OF CONTENTS:
  1. Using Code Igniter to Enable Comments in a Blog Application
  2. The application so far
  3. Inserting comments with a Blogger controller class method
  4. Redefining the blogs_comment_view.php file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
December 30, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: