Home arrow PHP arrow Page 4 - Displaying User Comments in a Code Igniter Blog Application

Creating a view file to display user-submitted comments - PHP

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.

TABLE OF CONTENTS:
  1. Displaying User Comments in a Code Igniter Blog Application
  2. Review: displaying paginated blog entries
  3. Displaying user-supplied comments
  4. Creating a view file to display user-submitted comments
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
December 23, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: