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.

  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



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


// load database class and connect to MySQL


// load pagination library


// load helper



// display all blog entries

function blogs(){

$data['title']='Blog Entries Listing';


// set pagination parameters




$config['full_tag_open']='<div id="paginglinks">';



// create pagination links


// load 'blogs_view' view



// display all blog comments

function comments(){

$data['title']='Blog Comments Listing';



// load 'blogs_comment_view' view



// insert new blog comment

function insert_comment(){





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


- 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: