Building a Content Management System with Code Igniter

Welcome to the conclusion of a nine-part series focused on showing you how to build applications with the Code Igniter framework. Designed to allow programmers to create applications rapidly by utilizing the Model-View-Controller pattern, the Code Igniter framework lets you add many capabilities quickly and easily. This article will bring everything we’ve learned together for the creation of a content management system.


If you’re a PHP programmer who’s seeking a framework that lets you build full-blown web applications very rapidly, without having to deal with complex installation issues, then you might want to look at Code Igniter ( It is a friendly software package developed in PHP 4 that permits you to create web-based programs by using the Model-View-Controller pattern.

Naturally, if you’ve read the articles that preceded this one, then you have the background necessary to start building object-based PHP applications with Code Igniter. This framework provides developers with the right tools for constructing anything that they can think of, ranging from simple data validation programs to powerful database-driven applications.

Now that I’ve mentioned the remarkable capabilities offered by Code Igniter when it comes to rapid application development, you will no doubt remember that in the previous article I created a simple web-based program that could send email messages by utilizing a regular HTML form.

Regardless of the intrinsic simplicity of this application, it provided a pretty useful demonstration of how easy it is to build such an application with the MVC approach. Basically, this email program was comprised of only one controller class, and two view files as well. It really was that simple.

However, until now I’ve only shown you how to perform certain specific tasks, such as validating user-supplied data and sending email, handling and paginating database records, and so forth. But, as you may guess, all of these isolated procedures can be used perfectly together to develop a larger, real-world application.

Therefore, with this idea in mind, this last episode of the series will be focused on building a primitive content management system with Code Igniter. This particular CMS will come in handy for adding comments about a group of popular movies previously stored in a MySQL database table.

Now we will start developing this movie-related management system. Hollywood, here we go!

{mospagebreak title=Creating sample MySQL tables and defining a model class}

We’ll begin building this content management system with Code Igniter by creating a couple of basic MySQL tables. The first one will be used for storing information about a few movies, including the corresponding titles and a brief description, while the second table will be utilized for inserting comments about each of them.

Based on this database schema, the PHP application that I plan to develop will let users enter several comments on a particular movie by means of an HTML form.

Having clarified this point, I’m going to create the first sample MySQL table, which will be called “movies,” with a structure that looks like this:

As you can see, the “movies” table contains data about a few relatively recent movies, and it’s very simple to grasp. However, there is still one more step to take. We need to create an additional table for entering comments on each of the previous movies.

The empty structure of this second table, called “comments,” can be seen below:

Now that the two sample tables have been properly created, everything is ready for us to begin developing this movie-related content management system. Therefore, to interact with these tables, I’m going to define a model class that permits us to achieve this goal in a simple manner.

The signature of this model class is as follows:

class MovieModel extends Model{

function MovieModel(){

// call the Model constructor


// load database class and connect to MySQL



// fetch all rows

function fetchAllRows($table){



// return result set as an associative array

return $query->result_array();



// fetch rows based on a certain condition

function fetchRow($param,$field,$table){



// return result set as an associative array

return $query->result_array();


// insert row

function insertRow($table,$data){



// get total number of rows

function getNumRows($table){

return $this->db->count_all($table);



As illustrated above, the logic that drives the previous “MovieModel” class is fairly easy to understand. Basically, all that this class does is implement a group of methods for fetching and inserting rows into a selected MySQL table, as well as for returning the total number of records contained within a result set.

This model class should be saved to the Code Igniter /system/application/models/ folder as “moviemodel.php” for later use.

Okay, assuming that you’ve read the previous articles of this series, then you should be pretty familiar with the structure of the above model class, right? Therefore, it is time to take the next step involved in the development of this content management system, which obviously consists of defining a controller.

As you’ll see in a moment, the controller will be responsible for displaying all of the movies contained in the “movies” MySQL table that you saw before. It will also let users enter different comments on each of them, via a simple HTML form.

To see how this brand new controller class will be created, please visit the following section and keep reading.

{mospagebreak title=Building a controller class}

True to form, building a controller class that fetches all the movies included in the previous “movies” MySQL table, and also allows users to enter comments about each of them, simply involves defining a different method for performing each of these tasks.

However, you will gain a better understanding of the way that this controller class is going to work if you examine its signature, which has been included below. Take a look at it, please:

class Movies extends Controller{

function Movie(){

// load controller parent


// load ‘MovieModel’ model


// load helpers




// display all movies

function index(){

$data['title']=’Movies List’;


// load ‘movie_view’ view



// display all comments

function comments(){

$data['title']=’Comment List’;


// load ‘moviecomment_view’ view



// insert comment

function insert_comment(){





Although the definition of the above “Movie” controller looks pretty intimidating at first, it’s actually pretty simple to understand. First, its constructor loads all of the source classes and functions that will be used within the management system; this includes the corresponding model, as well as the “url” and “form” helpers that you learned in previous articles of this series.

Now, take a look at the “index()” method. As you’ll recall, it will be called automatically by Code Igniter when implemented, and it is tasked with displaying on screen all of the movies included in the “movies” MySQL table defined before.

As you can see, this method first fetches all of the movies from the aforementioned table by means of the model, and lastly it embeds this data into a view file called “movie_view.php” for displaying purposes. For right now, don’t worry about how this view looks, since this topic will be discussed in depth in the following section.

At this level, you hopefully grasped the logic implemented by the pertinent “index()” method, right? So focus your attention now on the one called “comments().” As its name suggests, this method is responsible for fetching all of the comments that belong to a specific movie. This task is performed by means of a conditional WHERE SQL statement, which is logically hidden behind the model’s API.

In addition, you should notice that this method retrieves the comments made on a particular movie by using a parameter passed in within the URL, whose value is returned by the following expression:


Actually, Code Igniter will automatically load a class called “uri” with each controller defined, so in this case its “segment()” method is utilized to retrieve the ID of the movie that has received a comment.

And finally, the “insert_comment()” method again uses the model’s API, this time for adding a new entry to the “comments” MySQL table created in the previous section. Of course, the text and author that correspond to each comment are collected via a post HTML form, therefore the following expressions:



first add the comment in question to the corresponding “comments” MySQL table, and then redirect users back to the comment’s web page.

Now, before I forget, please save the controller class to the Code Igniter /system/application/controllers/ as “movies.php” folder for further use.

So far, so good, right? At this point, you should have a clear idea of how the previous “Movies” controller class does its thing. Also, it’s possible that you still have some doubts regarding the way that movies and comments are displayed on the browser, or even how new comments are submitted by a user.

However, you shouldn’t be too concerned about this. In the next section, I’ll be listing all of the view files that are required to perform all the aforementioned tasks.

Want to see how this will be done? Click on the link below and keep reading.

{mospagebreak title=Completing the CMS with view files}

To get this movie-related content management system completed, it’s necessary to create all of the view files that are used for the previous “Movies” controller class to display the list of available movies and their corresponding comments, as well as the HTML form that permits the insertion of these comments into the corresponding MySQL table.

That being said, here’s the definition of the first view file, called “movie_view.php.” It is utilized by the controller to display the list of movies available for receiving comments. Have a look at it, please:



<title><?php echo $title;?></title>



<h1><?php echo $title;?></h1>

<?php foreach($movies as $movie):?>

<h2><?php echo $movie['name'];?></h2>

<p><?php echo $movie['description'];?></p>

<p><?php echo anchor(‘movie/comments/’.$movie['id'],’View Comments’);?></p>

<hr />

<?php endforeach;?>



Definitely, the above view file is quite simple to grasp. As you can see, this file will display all of the movies stored in the database. It includes a link that will take users to the comments web page, so they can submit a new comment for each particular movie.

Besides, you may want to look at the following screen help, which shows precisely how movies are displayed on screen:

That image was pretty illustrative, right? Now it’s time to include the view file that renders the comments web page, which not surprisingly is called “moviecomment_view.php.” Here it is:



<title><?php echo $title;?></title>



<h1><?php echo $title;?></h1>

<?php foreach($comments as $comment):?>

<p><?php echo $comment['author'];?></p>

<p><?php echo $comment['text'];?></p>

<p><?php echo anchor(‘movie’,'Back to movies’);?></p>

<hr />

<?php endforeach;?>

<?php echo form_open(‘movie/insert_comment’);?>

<?php echo form_hidden(‘movie_id’,$this->uri->segment(3));?>

<p>Enter your comments below:</p>

<p><textarea name="text" rows="10"></textarea></p>


<p><input type="text" name="author" /></p>

<p><input type="submit" value="Submit Comment" /></p>




As you can see, the above view file performs a couple of crucial tasks, such as displaying all the comments that have been submitted for a specific movie, and providing a basic HTML form, which lets users add new entries.

Of course, you’ll understand much more clearly how this view works, if you look at the following screen capture:

See how easy it is to provide users with an interface that lets them enter comments on a specific movie? I guess you do! Naturally, these two view files should be saved to the Code Igniter /system/application/views/ folders, so that they can be loaded at runtime by the previous controller class.

Okay, at this point I have finished building this simple content management system with Code Igniter. If you want to test it, you should type the following URL in your browser’s address field:


That’s all. If all of the source files created earlier were saved to the correct locations in the web server, then the application should work flawlessly!

Finally, feel free to introduce your own modifications to the files, so you can extend your Code Igniter skills to developing database-driven applications.

Final thoughts

Sad but true, we’ve come to the end of this series. The journey has been long, but hopefully instructive too, since you learned how to use Code Igniter to develop several useful web applications very quickly.

So, if you’re looking for a PHP framework that requires minimal setup and an easy learning curve, then Code Igniter might be the piece of software that you really need.

See you in the next PHP development tutorial!

Google+ Comments

Google+ Comments