CodeIgniter -向帖子添加评论

fd3cxomn  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(185)

我想请一些帮助。我有一个帖子页面,有完整的帖子和下面的帖子一个小的形式添加评论。帖子页面的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-〉评论模型-〉add($post_id);验证失败,在我的帖子视图中显示验证错误,否则插入评论并重定向到相同的帖子页面(site/posts/1)。
问题是,当我点击submit时,表单操作进入comments/add/1,正如预期的那样,但没有执行上述任何操作。
我该怎么办?”

EDIT我对代码做了一个小的修改,去掉了“令人困惑的”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;
}
dgsult0t

dgsult0t1#

您需要将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;

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

相关问题