Create a simple blog using CodeIgniter 2 – Part 2

CodeIgniter Logo

In this CodeIgniter tutorial, I’m going to show you how to create a blog post with comment function with CodeIgniter 2. You can download the code or just preview the final product first. In this version of code, I’ve created a simple template for our blog.

You can preview the part 1 of this tutorial at Create a simple blog using CodeIgniter 2 – Part 1.

1. Create a table for comment

CREATE TABLE IF NOT EXISTS `comment` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`entry_id` int(11) NOT NULL,
`comment_name` varchar(255) NOT NULL,
`comment_email` varchar(255) NOT NULL,
`comment_body` text NOT NULL,
`comment_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

2. Edit your Blog controller by adding a function called post.

//this function will retrive a post
public function post($id)
{
    $data['query'] = $this->blog_model->get_post($id);
    $data['comments'] = $this->blog_model->get_post_comment($id);
    $data['post_id'] = $id;
    $data['total_comments'] = $this->blog_model->total_comments($id);

    $this->load->helper('form');
    $this->load->library(array('form_validation','session'));

    //set validation rules
    $this->form_validation->set_rules('commentor', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('comment', 'Comment', 'required');

    if($this->blog_model->get_post($id))
    {
        foreach($this->blog_model->get_post($id) as $row)
        {
            //set page title
            $data['title'] = $row->entry_name;
        }

        if ($this->form_validation->run() == FALSE)
        {
            //if not valid
            $this->load->view('blog/post',$data);
        }
        else
        {
            //if valid
            $name = $this->input->post('commentor');
            $email = strtolower($this->input->post('email'));
            $comment = $this->input->post('comment');
            $post_id = $this->input->post('post_id');

            $this->blog_model->add_new_comment($post_id,$name,$email,$comment);
            $this->session->set_flashdata('message', '1 new comment added!');
            redirect('post/'.$id);
        }
    }
    else
        show_404();
}

3. Add new functions to your blog_model such as function to get a post, add new comments, get list of comments, and count total comments.

function add_new_comment($post_id,$commentor,$email,$comment)
{
    $data = array(
        'entry_id'=>$post_id,
        'comment_name'=>$commentor,
        'comment_email'=>$email,
        'comment_body'=>$comment,
    );
    $this->db->insert('comment',$data);
}

function get_post($id)
{
    $this->db->where('entry_id',$id);
    $query = $this->db->get('entry');
    if($query->num_rows()!==0)
    {
        return $query->result();
    }
    else
        return FALSE;
}

function get_post_comment($post_id)
{
    $this->db->where('entry_id',$post_id);
    $query = $this->db->get('comment');
    return $query->result();
}

function total_comments($id)
{
    $this->db->like('entry_id', $id);
    $this->db->from('comment');
    return $this->db->count_all_results();
}

4. Take note that I’ve configured my route.php so that I can make my URLs cleaner and shorter than before.

$route['about'] = 'blog/about';
$route['new-post'] = 'blog/add_new_entry';
$route['post/(:num)'] = 'blog/post/$1';

5. As you can see in views/blog folder, I’ve devided the segment of codes into few different files such as header, menu, comment, and footer so that it will be easier for me to edit the template in the future.

Please try to double check your database.php in config folder and use autoload to load database library to avoid problems such as unable to connect, database not found, etc.

Happy coding with CodeIgniter! Comments and feedbacks are most welcome. =D

P/s: This tutorial was written and published in May 2011.