为什么我尝试在不添加 Name and/or 正文的情况下提交评论时出现此错误?
Why do I have this error when I try to submit a comment without adding Name and/or body?
我正面临这个问题(请查看图片)。
我希望当我单击“提交”而不输入正文 and/or 电子邮件时,以显示这些字段是必需的。我目前是初学者,我试图处理错误,但我做不到。请帮助。
这是我的模型:
<?php
class Comment_model extends CI_Model {
public function __construct() {
$this->load->database(); //database library loaded
}
public function create_comment($post_id) {
$data = array(
'post_id' => $post_id,
'name' => $this->input->post('name'),
'body' => $this->input->post('body')
);
return $this->db->insert('comments', $data);
}
public function get_comments($post_id){
$query = $this->db->get_where('comments', array('post_id' => $post_id));
return $query->result_array();
}
}
这是我的评论管理员:
<?php
class Comments extends CI_Controller {
public function create($post_id) {
$slug = $this->input->post('slug');
$data['post'] = $this->post_model->get_posts($slug);
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
else {
$this->comment_model->create_comment($post_id);
redirect('posts/'.$slug);
}
}
}
Post 控制器:
public function view($slug = NULL) {
$data['post'] = $this->post_model->get_posts($slug);
$post_id = $data['post']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
并查看:
code
<h3>Comments</h3>
<?php if($comments) : ?>
<?php foreach($comments as $comment) : ?>
<div class="jumbotron bg-dark">
<h5><?php echo $comment['body']; ?> [by <strong><?php echo $comment['name']; ?></strong>]</h5>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>No Comments to Display</p>
<?php endif; ?>
您的评论控制器中缺少来自 $data 的 comments
。
使用 empty()
检查您的视图中是否存在变量
在你看来这样试试
<?php if(!empty($comments)) : ?>
而不是
<?php if($comments) : ?>
我正面临这个问题(请查看图片)。 我希望当我单击“提交”而不输入正文 and/or 电子邮件时,以显示这些字段是必需的。我目前是初学者,我试图处理错误,但我做不到。请帮助。
这是我的模型:
<?php
class Comment_model extends CI_Model {
public function __construct() {
$this->load->database(); //database library loaded
}
public function create_comment($post_id) {
$data = array(
'post_id' => $post_id,
'name' => $this->input->post('name'),
'body' => $this->input->post('body')
);
return $this->db->insert('comments', $data);
}
public function get_comments($post_id){
$query = $this->db->get_where('comments', array('post_id' => $post_id));
return $query->result_array();
}
}
这是我的评论管理员:
<?php
class Comments extends CI_Controller {
public function create($post_id) {
$slug = $this->input->post('slug');
$data['post'] = $this->post_model->get_posts($slug);
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
else {
$this->comment_model->create_comment($post_id);
redirect('posts/'.$slug);
}
}
}
Post 控制器:
public function view($slug = NULL) {
$data['post'] = $this->post_model->get_posts($slug);
$post_id = $data['post']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
并查看:
code
<h3>Comments</h3>
<?php if($comments) : ?>
<?php foreach($comments as $comment) : ?>
<div class="jumbotron bg-dark">
<h5><?php echo $comment['body']; ?> [by <strong><?php echo $comment['name']; ?></strong>]</h5>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>No Comments to Display</p>
<?php endif; ?>
您的评论控制器中缺少来自 $data 的 comments
。
使用 empty()
检查您的视图中是否存在变量
在你看来这样试试
<?php if(!empty($comments)) : ?>
而不是
<?php if($comments) : ?>