Home arrow PHP arrow Page 4 - Using Code Igniter to Enable Comments in a Blog Application

Redefining the blogs_comment_view.php file - PHP

Welcome to the fourth episode of the series entitled “Building a Blogger with the Code Igniter PHP framework.” Comprised of seven approachable articles, this series steps you through the development of a blog application using this friendly – yet powerful -- PHP framework. We include copious code samples to demonstrate how to use the MVC pattern to develop a real-world program.

TABLE OF CONTENTS:
  1. Using Code Igniter to Enable Comments in a Blog Application
  2. The application so far
  3. Inserting comments with a Blogger controller class method
  4. Redefining the blogs_comment_view.php file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
December 30, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Coding a simple web form that permit users to post their comments on a particular blog entry is a no-brainer process that only requires you to modify the signature of the "blogs_comments_view.php" file that you saw before.

In this case, I'm going to use Code Igniter's "form" helper function to dynamically generate the "action" attribute of the form, and also to create a hidden field that permits us to save the ID of the entry being commented.

If the previous explanation sounds rather confusing to you, then the following code sample should dissipate any possible doubts that you might still have:

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

<?php echo form_open('blogger/insert_comment');?>

<?php echo form_hidden('blog_id',$this->uri->segment(3));?>

<p>Author:</p>

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

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

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

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

</form>

</body>

</html>


Here you have it. Now the above view file includes a basic online form that allows users to submit their comments on a particular blog entry. In addition, it's worthwhile to notice that the ID value of each entry is retrieved via the Code Igniter "uri" class, which is loaded automatically by its default controller.

And finally, here's an additional screen capture that shows how this view file is rendered by the browser, after including into it the previous HTML form:



Before you proceed to read the conclusion of this article, don't forget to save this modified version of the view to the Code Igniter /system/application/views/ folder, so it can be loaded by the pertaining controller class.

Assuming that you have saved the file, it's time to test the improved blogger, right? Please point your browser to the following URL:


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


Now, click on the link that takes you to the comments web page and simply use the online form to start posting new entries. If everything has been set up correctly, then you'll be amazed how sweetly this blogger works.

Final thoughts

In this fourth episode of the series, I finished developing this simple blog application using Code Igniter. Logically, there's plenty of room to introduce your own improvements and features to suit your personal needs; due to the program's flexible structure, this should be a fairly painless process.

In the next part, I'll be polishing the visual presentation of the blogger, since in its current state, it looks pretty primitive. So, now that you know what the upcoming article will be about, you can't miss it!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: