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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $title;?></title>
<h1><?php echo $title;?></h1>
<?php foreach($result->result_array() as $comment):?>
<p><strong>Author: </strong><?php echo $comment['author'];?></p>
<p><?php echo $comment['text'];?></p>
<p><?php echo anchor('blogger/blogs/','<< Back to blog');?></p>
<?php echo form_open('blogger/insert_comment');?>
<?php echo form_hidden('blog_id',$this->uri->segment(3));?>
<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>
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:
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.
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!
blog comments powered by Disqus