Building a Blogger with the Code Igniter PHP Framework

People love to communicate, which may be why one of the most popular web applications you can develop is a blogger. In this six-part series, you’ll learn how to develop a blogging application using PHP and the Code Igniter framework. In this first article, we’ll create the bare bones structure, with plenty of code samples to help you understand the process.

Introduction

Although instant messaging applications rule the universe of real time, Internet-based communications, a blogging application provides users with a useful and simple way to share personal ideas, thoughts, interests, and so forth with other people. Besides, building a blog application is by far much easier than creating a messenger program, particularly when using a friendly programming language like PHP.

I don’t mean to imply, however, that it’s possible to create a blogger in a few minutes in all the cases. Whether you’re using a procedural programming methodology or an object-oriented approach, building an application like this requires time and an intermediate background in working with database-driven programs.

Even so, it’s perfectly feasible to take advantage of a third-party PHP framework to accelerate considerably the development of any web application, and a blogger falls under this category. Of course, there are several software packages available on the web that permit you to build PHP programs very rapidly, but in this particular case I’m going to demonstrate how to create a blog application with Code Igniter (for more information visit its official web site at http://code igniter.com).


In case you haven’t heard about it, Code Igniter is a powerful, comprehensive framework written in PHP 4 that provides developers with a robust set of classes and helpers that can be used for building different web applications by means of the Model-View-Controller pattern, which separates application logic from visual presentation.

Thus, in this series of articles I’m going to explain how to build a basic blogger with Code Igniter. I will explain it in a step-by-step fashion to allow you to get an excellent grounding in its main features, as well as learn how to implement the MVC approach in a real-world situation.

Also, it’s fair to mention that the implementation of the core logic of this blog program will rely partially on the video tutorial available on Code Igniter’s web site (so, many thanks to CI’s development team for offering such an educational introduction to using the framework). I will hasten to add that the rest of the application’s modules, including its visual presentation, record set paginating and other features will be largely improved.

That being said, it’s time to get rid of the preliminaries and start developing this blog program using Code Igniter. Let’s jump in!

{mospagebreak title=Start building the blogger with two MySQL database tables}


Since the blog application that I plan to construct will store all of the blog entries and the respective comments on a couple of MySQL tables, the first step will consist of creating these tables.

In this case, the first of these database tables will be called "blogs," and its structure will look similar to this:



As shown above, the "blogs" database table comprises three basic fields: the corresponding ID, defined as the primary key; a title; and finally the text of the blog itself. In addition, you can see that it has already been populated with some trivial data, but this step is completely optional. So if you want to start building the blogger with an empty table, then go ahead and do it.


Now that I have defined the first "blogs" MySQL table, it’s time to create another one, which will be used to store the comments about each blog entry. Sounds pretty logical, right? Therefore, here’s the empty structure of this brand new table, which not surprisingly is called "blogs_comments." Take a look at it, please:



As you can see, the above table will be utilized for storing the comments on each blog entry. In this case, each comment submitted by a user will include the author and the corresponding text as well. In addition, you may have noticed that this table contains a "blog_id" field. This field will be utilized as a foreign key to link this table with the previous one.

So far, so good. At this stage, I have created two simple MySQL tables that will be employed by the blog application, first for displaying a group of existing blog entries, and then for posting different comments on each of them.

The next step involves defining a controller class. This class will initially be responsible for displaying all of the blog entries stored in the "blogs" MySQL table that you saw before.

The details of this process will be covered in the following section. Therefore, jump forward and read the next few lines.

{mospagebreak title=Building a blog controller class to display blog entries}


As I anticipated in the previous section, the workhorse of this blog application will be represented by a controller class. This class will handle all of the tasks required by the program, such as listing all the existing blog entries and their corresponding comments, as well as displaying an HTML form that will let users to submit new posts.

However, for now let me show you the initial version of this controller, which momentarily will be tasked with displaying all of the blog entries stored on the "blogs" MySQL table. Here’s how this class looks:


// define ‘Blogger’ controller class


class Blogger extends Controller{

function Blogger(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

// load helpers

$this->load->helper(‘url’);

}

// display all blog entries

function blogs(){

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

$data['result']=$this->db->get(‘blogs’);

// load ‘blogger_view’ view

$this->load->view(‘blogs_view’,$data);

}

}


As illustrated above, the "Blogger" controller performs a few useful tasks that deserve a closer analysis. First, its constructor loads Code Igniter’s database class, and connects to MySQL by using the settings of its "config.php" file (for more information on how to install and configure Code Igniter, please read the user manual).

Once the database class has been included and it is working correctly, the constructor finishes its execution by loading the "url" helper function, which will be used later on for generating dynamic links.

Now that you have grasped how the controller’s constructor does its business, it’s time to look into the "blogs()" method, since its implementation is pretty interesting. As you can see, it first fetches all of the blogs entries from the previous "blogs" MySQL table and embeds this data directly into a view file, called "blogs_view.php," to be displayed on screen.

In case you didn’t know, this is the approach utilized by Code Igniter to implement the MVC pattern, so if you’re not familiar with this concept, please check the framework’s user manual.

Before I move on and continue building this blog application, the previous controller should be saved to the Code Igniter /system/application/controllers/ folder as "blogger.php," so that it can be invoked correctly.

Done? All right, I have now built a controller class that’s capable of displaying on the browser a list of blog entries. Well, actually, this isn’t entirely accurate, since there’s still an additional step that must be taken.

Yes, you guessed right! As I explained before, the controller loads a view file to display all of the blog entries, meaning that it is first necessary to create this file. Therefore, in the section to come I’ll be showing you the details of this process.

Please click on the link below and keep reading.

{mospagebreak title=Defining a simple view file}

In the previous section, I explained how to build a controller class that was provided with the capacity to display all of the blog entries stored in the "blogs" MySQL table. Of course this operation can’t be performed correctly if I don’t first create the corresponding view file that actually renders this blog-related data.

Thus, taking into account this requisite, below I included the signature of this view, which looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

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

</head>

<body>

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

<?php foreach($result->result_array() as $blog):?>

<h2><?php echo $blog['title'];?></h2>

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

<p><?php echo anchor(‘blogger/comments/’.$blog['id'],’View Blog Comments &gt;&gt;’);?></p>

<?php endforeach;?>

</body>

</html>


Despite the short signature of the above view file, it actually performs a few useful tasks that are worth examining in detail. As you can see, this file starts displaying all the corresponding blog entries via a "foreach" loop, including their titles and texts, and it finishes its execution by creating dynamically a link that points to a comments section, in this case by using Code Igniter’s "url" helper function.

Naturally, this section hasn’t been created yet, and it must be implemented within the previous controller class. However, since this topic will be discussed in forthcoming tutorials of this series, you shouldn’t be concerned about it for the moment.

Now that you have hopefully grasped how the previous view file works, you should save it to the Code Igniter /system/application/views/folder as "blogs_view.php."

So far, everything looks good, since at this stage I demonstrated how to build a simple blog application, which for the moment is only capable of displaying a group of blog entries stored previously in a MySQL table.

If you’re like me, then you may want to test the application in question. To do this, simply type the following URL into your browser’s address field:

http://localhost/codeigniter/index.php/blogger/blogs/


If all goes well, you should get the following output on your browser:


This is the title of the first blog


This is the content of the first blog. This is the content of the first blog. This is the content of the first blog. This is the content of the first blog.

View Blog Comments >>



This is the title of the second blog


This is the content of the second blog. This is the content of the second blog. This is the content of the second blog. This is the content of the second blog.

View Blog Comments >>



This is the title of the third blog


This is the content of the third blog. This is the content of the third blog. This is the content of the third blog. This is the content of the third blog.

View Blog Comments >>



This is the title of the fourth blog


This is the content of the fourth blog. This is the content of the fourth blog. This is the title of the fourth blog. This is the title of the fourth blog.
View Blog Comments >>



This is the title of the fifth blog


This is the content of the fifth blog. This is the content of the fifth blog. This is the title of the fifth blog. This is the title of the fifth blog. This is the title of the fifth blog.
View Blog Comments >>



This is the title of the sixth blog

This is the content of the sixth blog. This is the content of the sixth blog. This is the content of the sixth blog. This is the title of the sixth blog.
View Blog Comments >>


This is the title of the seventh blog

This is the content of the seventh blog. This is the content of the seventh blog. This is the content of the seventh blog. This is the title of the seventh blog.
View Blog Comments >>


This is the title of the eight blog

This is the content of the eight blog. This is the content of the eight blog. This is the content of the eight blog. This is the title of the eight blog.
View Blog Comments >>


This is the title of the ninth blog

This is the content of the ninth blog. This is the content of the ninth blog. This is the content of the ninth blog. This is the title of the ninth blog.

View Blog Comments >>



This is the title of the tenth blog


This is the content of the tenth blog. This is the content of the tenth blog. This is the content of the tenth blog. This is the title of the tenth blog.

View Blog Comments >>


That was pretty satisfactory, wasn’t it? As shown above, this initial version of the blogger works fairly well, since it’s capable or displaying all the blog entries stored on the aforementioned "blogs" MySQL table. And naturally, this process has been accomplished by using only a controller and a basic view file.

What else can you ask for? Well, actually there’s many more features that need to be added to the blogger, but they’ll be incorporated progressively in successive articles of this series. For the moment, study all of the examples coded here, so you can start familiarizing yourself with developing database-driven programs using Code Igniter.

Final thoughts

In this first chapter of the series I explained how to build a basic blog application with the Code Igniter PHP framework. As you saw before, this process was pretty straightforward; it only required the creation of a controller class and a view file. That was all.

In the next part, I’ll be showing you how to use the pagination class that comes bundled with Code Igniter, in order to spawn across several pages all of the blog entries shown previously. Don’t miss the upcoming article!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan