[英]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))
. post页面的uri是:site/posts/1,所以它在posts控制器中,表单动作是
form_open(site_url('comments/add/'.$post->post_id))
。
This is my add() function inside comments controller:这是我在评论控制器中的 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
}
and this is the add() function inside the comment model这是注释模型中的 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;
}
}
What I'm trying to do is if the $result = $this->comment_model->add($post_id);我想要做的是如果 $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).
验证失败以在我的帖子视图中显示验证错误,否则插入评论并重定向到同一帖子页面(站点/帖子/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.问题是,当我点击提交时,表单操作按预期进入评论/添加/1,但没有执行上述任何操作。
Any ideas how can I fix this??任何想法我该如何解决这个问题?
EDIT I did a small change to the code without the 'confusing' validate() function.编辑我对代码做了一个小的改动,没有“令人困惑”的 validate() 函数。 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;
}
You need away of passing validation_errors()
back to your Posts
controller.您需要将
validation_errors()
传递回您的Posts
控制器。 At the minute, when you perform the redirect in your add
function (when the validation fails), you loose the validation errors thrown.此时,当您在
add
函数中执行重定向时(当验证失败时),您会丢失抛出的验证错误。
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.我会考虑使用
flashdata
( http://ellislab.com/codeigniter/user-guide/libraries/sessions.html )将成功/错误消息从您的Comments
控制器传递回您的Posts
控制器。 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...可能不完美,但希望对您有所帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.