我是codeigniter的初学者,我正在学习基本的知识。我正面临一个在index.php中的错误
<h2><?=$title?></h2>
<?php foreach($posts as $post)?>
<h3><?php echo $post['title'];?></h3>o
<small class="post-date">Posted on:<?php echo $post['created at'];?> </small><br>
<?php echo word_limiter($post['body'],60);?>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
<?php endforeach;?>
遇到PHP错误 严重性:通知
消息:未定义的变量:帖子
文件名:posts / index.php
行号:2
回溯:
文件:/opt/lampp/htdocs/codeigniter/application/views/posts/index.php
行:2
功能:_error_handler文件:/opt/lampp/htdocs/codeigniter/application/controllers/Posts.php
行:23
功能:查看文件:/opt/lampp/htdocs/codeigniter/index.php
线:315
功能:require_once
我的控制器是
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$data['posts']=$this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug=NULL)
{
$data['post'] = $this->Post_model->get_posts($slug);
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');
}
public function create(){
$data['title'] = 'create post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','body','required');
if($this->form_validation->run() === false) {
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
} else {
$this->Post_model->create_post();
redirect('posts');
}
}
public function delete($id) {
$this->Post_model->delete_post($id);
redirect('posts');
}
public function edit($id) {
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function update()
{
$this->Post_model->update_post();
redirect('posts');
}
}
我的模型文件是
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = false){
if($slug === false) {
$this->db->order_by('id','DESC');
$query=$this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug' => $slug));
return $query->row_array();
}
public function create_post(){
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('posts',$data);
}
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
public function update_post() {
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
}
答案 0 :(得分:0)
在控制器上的编辑功能中,您有edit($id)
但是,如果您加载模型,则get_posts($slug)
应为get_posts($id)
并将
$data['post']
更改为$data['posts']
public function edit($id) {
$data['posts'] = array();
// rename slug to id
$data['posts'] = $this->post_model->get_posts($id);
if(empty($data['posts'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
/*
Tip You don't have to use index.php on for view you could create a
another view instead $this->load->view('posts/edit_view',$data);
*/
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
在控制器上的何时加载模型
更改
$this->Post_model->get_posts($id);
要
$this->post_model->get_posts($id);
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
On Autoload.php
更改
$autoload['model'] = array('Post_model');
要
$autoload['model'] = array('post_model');
在autoload.php中加载数据库可以让生活更轻松
$autoload['libraries'] = array('database');