CodeIgniter Forums
Adding comments to a post - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Adding comments to a post (/thread-59619.html)



Adding comments to a post - El Forum - 10-28-2013

[eluser]Lykos22[/eluser]
Hi all, I'd like some help please, I 've completly stuck!

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:
Code:
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) {
   $this->comment_model->add($post_id);
   redirect('posts/'.$post_id);
  }

    // TODO:load the view if required
}

and this is the add() function inside the comment model:
Code:
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;
}

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?? Can also found here


Adding comments to a post - El Forum - 10-29-2013

[eluser]rappasoft[/eluser]
You seem to be returning an id on success but not checking if it was false or not:

This:

Code:
if ($this->form_validation->run() == true) {
   $this->comment_model->add($post_id);
   redirect('posts/'.$post_id);
  }

Could be this:

Code:
if ($this->form_validation->run() == true) {

   if ($id = $this->comment_model->add($post_id)) {
       redirect('posts/'.$id);
   } else {
        //Returned false, show errors
   }
  
}



Adding comments to a post - El Forum - 10-29-2013

[eluser]Lykos22[/eluser]
The problem was that I wasn't passing the validation_errors() back to the Posts controller, due to the redirect all the validation errors were lost. So the only solution I found was to store them in flashdata and send them back to the Posts controller.

However if there's another better way I'd be grateful if you could inform me.