Displaying User Comments in a Code Igniter Blog Application

Welcome to the third part of the six-part series titled “Building a Blogger with the Code Igniter PHP Framework.” In successive tutorials, this series shows you how to use this software package to develop a blog application by using the Model-View-Controller design pattern. In this article, you will learn how to make the application display user comments.

From a programmer’s point of view, building a blog application with PHP can be an extremely educational experience. This process generally requires working with MySQL, as well as creating scripts that must handle several result sets, update and delete database records, and so forth.

Developing such a web application is something that most programmers would love to do, as long as they don’t have to spend a lot of time writing new code. Fortunately, there are several PHP frameworks available on the web that allow users to develop PHP programs with minor hassles. Code Igniter (http://codeigniter.com) is one of the friendliest to use and learn.

So, if you’re interested in learning how to take advantage of the functionality provided by Code Igniter to build a blogger, then this series of articles might be what you’re looking for.

If you had the chance to read the previous article, then you’re becoming familiar with building a blogger using Code Igniter. In that tutorial I explained how to combine the functionality of a couple of files to display blog entries previously stored in a MySQL table.

In this specific case, the first of these files was defined as a basic controller, and was provided with the ability to paginate the aforementioned blog entries via the corresponding pagination class included with Code Igniter.

However, in its current incarnation, the blog application is pretty limited. It doesn’t let users post comments on each blog entry. Thus, in the next few lines I’ll be improving the signature of the controller class to address this important issue.

Are you ready to continue learning how to build a blogger with Code Igniter? Then let’s get going!

{mospagebreak title=Review: displaying paginated blog entries}

Before I proceed to modify the signature of the controller class that I built in the previous article, I’d like to list it in its current state, along with the corresponding view file. This will remind you of how these two source files looked before we make the appropriate changes to their respective signatures.

That being said, here are the definitions for the files in question:


// definition for ‘blogger.php’ file (located at /system/application/controllers/ folder)


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 ‘blogger_view’ view

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

}

}



// definition for ‘blogs_view.php’ file (located at /system/application/views/ folder)


<!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;?>

<?php echo $links;?>

</body>

</html>


As you can see, the above “Blogger” controller class implements a method called “blogs(),” which not only retrieves all the entries from the pertinent “blogs” MySQL table, but splits this data into several chunks. In this way it implements an effective pagination mechanism. 

Finally, the controller loads the view file shown above, which is used to display all of the blog entries on screen, along with the corresponding pagination links.

Okay, at this point you hopefully recalled how the two previous files do their business, right? Therefore, it’s time to see how to modify the signature of the controller class, and provide it with the capacity for displaying all of the  comments submitted by users.

The full details of this process will be discussed in the following section. Thus, click on the link below and keep reading, please.

{mospagebreak title=Displaying user-supplied comments}

To be frank, providing the previous “Blogger” controller class with the capacity for displaying all of the comments that have been made on each blog entry is only a matter of adding to the class a simple method which performs that specific task.

However, it’s probable that this concept will be easier to understand if you look at the modified signature of the controller class. It includes a brand new method, called “comments(),” tasked with showing all the comments posted for each blog entry. Here it is:

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 ‘blogger_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);

}

}


As I mentioned before, the above “Blogger” controller class now includes another method, not surprisingly called “comments().” It is responsible for fetching, from the pertinent “blogs_comments” MySQL table, all of the comments that have been posted about a particular blog entry.

In order to perform this task, the method executes a WHERE conditional clause against the table using the active record pattern. It’s worth noticing, by the way, that there are no SQL statements coded explicitly here.

And finally, once the corresponding database rows has been retrieved, then they’re embedded into a view file so they can be displayed at a later time. Obviously, if you analyze this process pretty closely, you may now be asking yourself the following question: how can users post their comments on one or more blog entries?

Well, I‘m glad you asked! As I explained a few lines above, the “comments()” method inserts the data retrieved from the “blogs_comments” MySQL table into a view file called “blogs_comment_view.php,” right? This file will include a simple HTML form which let users submit several comments on different blog entries. Now, are you starting to realize how this method does its thing? I bet you are!

However, to dissipate any possible doubts that you might have regarding how the “comments()” method works, in the section to come I’ll be listing the signature of the “blogscomments_view.php” file. You’ll be able to study it in detail.

In the meantime, save the modified version of the previous “Blogger” controller class to the Code Igniter /system/application/controllers/ folder as “blogger.php,” then click on the link that appears below and keep reading.

{mospagebreak title=Creating a view file to display user-submitted comments}

As I stated in the previous section, to finish developing this blog application with Code Igniter, it’s necessary to create another view file. This file will be charged with printing, on screen, all of the comments that have been submitted for each blog entry.

To demonstrate how this file really functions, I’m going to assume that a few trivial comments have already been stored in the pertinent “blogs_comments” MySQL table. However, in a typical situation, users should be able to post their comments via an HTML form. Since this topic will be covered in depth in the next tutorial of the series, for now I’ll keep the blog application working this way.

Now that I have explained the functionality of the “blogs_comment_view.php” file, please take some time to study its signature, 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 if($result->num_rows()>0):?>

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

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

<p><strong>Comment:</strong></p>

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

<p><?php echo anchor(‘blogger/blogs/’,'&lt;&lt; Back to blog’);?></p>

<?php endforeach;?>

<?php endif;?>

</body>

</html>


As illustrated above, the previous view file is responsible for performing a few interesting tasks, such as displaying all the comments that have been made on a particular entry, and including a link that takes users to the blogger’s main page.

In addition, you can see that each comment includes the author that submitted it and the corresponding text as well. Nonetheless, as I stated before, this operation should be performed through a typical web form; the details of this topic will be discussed in the next article in this tutorial series.

For now, the only step that remains undone is to save the previous view file to the Code Igniter /system/application/views/ folder, so go ahead and do that. Then, if you test the blog application and click on any of the links that points to the comments web page, you should get an output similar to this:



Naturally, the above image exemplifies a situation where some blog entries have already received comments, so you can see more clearly how the blogger works.

Okay, at this point, the initial functionality of this blog application has been slightly extended; it’s now capable of displaying not only all of the existing blog entries, but the comments that have been posted on each of them. As usual with many of my articles on PHP development, feel free to tweak all the code samples developed in this tutorial to expand your existing Code Igniter skills.

Final thoughts

In this third part of the series, I explained how to improve the functionality of this blogger by providing it with the capacity for displaying a bunch of comments that correspond to each blog entry. However, as I explained earlier, for entering new comments it is necessary to code a typical HTML form.

Therefore, in the following article I’ll discuss how to achieve this with Code Igniter. Now that you know what will be covered in the next tutorial, you don’t have any excuses to miss it!

Google+ Comments

Google+ Comments