這個問題在這里已經有了答案:
這是我的模型:
<?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);
}
}
}
后控制器:
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; ?>
uj5u.com熱心網友回復:
在你看來嘗試這樣
<?php if(!empty($comments)) : ?>
代替
<?php if($comments) : ?>
uj5u.com熱心網友回復:
您comments的評論控制器中缺少您的 from $data。用于empty()檢查變數是否存在于您的視圖中
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454507.html
上一篇:OSError:[Errno8]Execformaterror:'/home/ec2-user/Desktop/chromedriver'在AWSEC2ARM風味機器中使用Chro
