Nested Comments using Codeigniter, AJAX

In my previous tutorial Nested comment system in PHP, AJAX I have shown how to create nested or threaded comment system using PHP, MySQl and AJAX. Here I am going to show you how to do the same thing but using Codeigniter 3 framework.

The final output on the browser would be similar to the following attached screen-shot

nested comment using codeigniter, ajax

Now we will look into the following steps to create such comment system.


Netbeans 8.1
XAMPP in Windows
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Directory Structure

A typical directory structure for the project would be as shown below. Here assests directory will contain static resources like css, js, images.

nested categories codeigniter

Step 1. Now modify <root directory>/application/config/autoload.php file for auto-loading html, url, file, form and session

Create ci3/application/helpers/app_helper.php file with the following source code

Step 2. Go to location ci3/application/config/database.php file and change database parameter values for below parameters

Step 3. Create a MySQL table in “roytuts” database

Step 4. Dump some data into database table – blog and blog_comment

Step 5. Create a model file blogmodel.php under ci3/application/models directory with the below source code

Step 6. Create a controller file blogcontroller.php under ci3/application/controllers with the following source code

Step 7. Create a view file blog_details.php under ci3/application/views

Step 8. Modify file ci3/application/config/routes.php file

Step 9. Create blog_comments.js file under ci3/assets/js directory with below content

Step 10. If everything is fine then run the application. You will get the output in the browser.

Download assets assets

That’s all. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *