phpcodeignitercodeigniter-2codeigniter-url

CodeIgniter - adding comments to a post


I'd like some help please. I have a post page that has the full post and below the post a small form for adding comments. The uri of the post page is: site/posts/1, so it is in posts controller, and the form action is form_open(site_url('comments/add/'.$post->post_id)).

This is my add() function inside comments controller:

public function add($post_id){
    // if nothing posted redirect
    if (!$this->input->post()) {
        redirect(site_url());
    }

    // TODO: save comment in database
    $result = $this->comment_model->add($post_id);
    if ($result !== false) {
        redirect('posts/'.$post_id);
    }

    // TODO:load the view if required
}

and this is the add() function inside the comment model

public function add($post_id){
    $post_data = array(
        'post_id' => $post_id, 
        'username'  => $this->input->post('username'),
        'email'     => $this->input->post('email'),
        'comment'   => $this->input->post('comment')
    );

    if ($this->validate($post_data)) {

        $this->db->insert('comments', $post_data);

        if ($this->db->affected_rows()) {
            return $this->db->insert_id();
        }
        return false;
    } else {
        return false;
    }
}

What I'm trying to do is if the $result = $this->comment_model->add($post_id); fails the validation to display the validation errors in my post view, else insert the comment and redirect to the same post page (site/posts/1).

The problem is that when I hit submit the form action goes in the comments/add/1, as expected, but doesn't do any these above.

Any ideas how can I fix this??

EDIT I did a small change to the code without the 'confusing' validate() function. Maybe this is more helpful.

Comment controller:

 public function add($post_id){
    // if nothing posted redirect
    if (!$this->input->post()) {
        redirect(site_url());
    }

    // TODO: save comment in database
    $this->form_validation->set_rules($this->comment_model->rules);
  if ($this->form_validation->run() == true) {
    echo "Ok! TODO save the comment.";
   // $this->comment_model->add($post_id);
   // redirect('posts/'.$post_id);
  } else {
      echo "Validation Failed! TODO: show validation errors!";
  }

    // TODO:load the view if required
}

Comment model:

 public function add($post_id){
    $post_data = array(
        'post_id' => $post_id, 
        'username'  => $this->input->post('username'),
        'email'     => $this->input->post('email'),
        'comment'   => $this->input->post('comment')
    );

        $this->db->insert('comments', $post_data);

        if ($this->db->affected_rows()) {
            return $this->db->insert_id();
        }
        return false;
} 

Solution

  • You need away of passing validation_errors() back to your Posts controller. At the minute, when you perform the redirect in your add function (when the validation fails), you loose the validation errors thrown.

    I would consider using flashdata (http://ellislab.com/codeigniter/user-guide/libraries/sessions.html) to pass a success/error message from your Comments controller back to your Posts controller. Something similar to the below:

    Comments Controller:

    public function add($post_id) {
        // if nothing posted redirect
        if (!$this->input->post()) {
            redirect(site_url());
        }
    
        // TODO: save comment in database
        $this->form_validation->set_rules($this->comment_model->rules);
        if ($this->form_validation->run() == true) {
            // Store the success message in flash data
            $this->session->set_flashdata('message', 'Ok! TODO save the comment.');
            // Redirect back to posts page
            redirect('posts/'.$post_id, 'refresh');
        } else {
            // Store the error message in flash data
            $this->session->set_flashdata('message', validation_errors());
            // Redirect back to posts page
            redirect('posts/'.$post_id, 'refresh');
        }
    }
    

    Posts Controller:

    public function index($post_id) {
        $this->data['message'] = $this->session->flashdata('message');
        $this->load->view('posts', $this->data);
    }
    

    Posts View:

    echo $message;
    

    Might not be perfect but hope it helps...