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

The application so far - 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

Before I go straight to the point and show you how to incorporate into the blogger the ability to insert user comments into a MySQL table, first I'd like to list the two source files created in the previous article. They were actually the driving force of this web application. This way, you'll get a good grasp of how these files looked before we modify them as I explained earlier. 

That being clarified, below are the respective definitions for the controller class and the view file that make the blogger work as expected. Here they are:


// 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 'blogs_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);

}

}



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



// definition for 'blogs_comment_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 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 you can see, the controller class is the module responsible for fetching all of the blog entries and their corresponding comments from the respective MySQL tables. The two additional views are tasked with displaying this data in the form of HTML pages.

Nevertheless, I have to admit that the blogger in its current state offers limited functionality, since it doesn't provide users with a web form for posting their comments on a particular entry. To perform this operation, first it's necessary to define a brand new method within the controller, and second, the signature of the "blogs_comments_view.php" file must be modified to include the online form.

It sounds like hard work, right? Not when using Code Igniter! In the section to come I'll be showing you how to complete these tasks, so click on the link 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: