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

Displaying user-supplied 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

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.



 
 
>>> 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: