CodeIgniter - 为帖子添加评论

时间:2013-10-28 10:22:48

标签: php codeigniter codeigniter-2 codeigniter-url

我想请一些帮助。我有一个帖子页面有完整的帖子,帖子下面有一个小表格,用于添加评论。帖子页面的uri是:site / posts / 1,因此它位于posts控制器中,表单操作为form_open(site_url('comments/add/'.$post->post_id))

这是我在评论控制器中的add()函数:

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
}

这是注释模型中的add()函数

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;
    }
}

我要做的是,如果$ result = $ this-> comment_model-> add($ post_id);未通过验证在我的帖子视图中显示验证错误,否则插入评论并重定向到相同的帖子页面(site / posts / 1)。

问题在于,当我点击提交时,表单操作会按照预期进入comments / add / 1,但不会执行上述操作。

任何想法如何解决这个问题?

修改 我对代码做了一些小改动而没有'令人困惑'的validate()函数。也许这更有帮助。

评论控制器:

 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
}

评论模型:

 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;
} 

1 个答案:

答案 0 :(得分:0)

您需要将validation_errors()传递回Posts控制器。此时,当您在add函数中执行重定向时(验证失败时),您将丢失抛出的验证错误。

我会考虑使用flashdatahttp://ellislab.com/codeigniter/user-guide/libraries/sessions.html)将成功/错误消息从您的Comments控制器传回您的Posts控制器。类似于下面的东西:

评论控制器:

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');
    }
}

帖子控制器:

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

帖子查看:

echo $message;

可能不完美,但希望它有所帮助...

相关问题