创建我的模型,视图和控制器以尝试从我的数据库中获取数据后,我收到错误,并希望帮助找出我收到此错误的原因:
错误如下:
Severity: Notice
Message: Undefined property: How_can_we_help::$Content_model
Filename: controllers/how_can_we_help.php
Line Number: 14
我试图通过调用模型中的函数来获取数据,控制器看起来像:
class How_can_we_help extends CI_Controller {
public function index()
{
//subcategory of page
$data = array(
'subcategory' => 'how_can_we_help',
);
//Get data from content table where subcategory = subcategory
$data['pagecontent'] = $this->Content_model->getContent($data);
//inserts "how_can_we_help" view into template
$data['main_content'] = 'how_can_we_help';
$this->load->view('includes/template', $data);
}
}
我只想检索子类别= how_can_we_help
的数据型号:
class Content_model extends CI_Model {
function getContent($data){
$this->db->select('category, subcategory, title, intro, content, tags');
$this->db->from('content');
$this->db->where($data);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
最后是视图
<?php
foreach ($pagecontent->result() as $row)
{
$title = $row->title;
$intro = $row->intro;
$content = $row->content;
$tags = $row->tags;
}
;?>
有人可以向我展示我的方式错误。
蝙蝠侠
答案 0 :(得分:2)
您在使用之前加载模型吗?
看起来CI错误地认为$this
引用认为它是他的方法之一,而它指的是模型。请确保您及时加载模型或在application / config / autoload.php中自动加载模型:
public function index()
{
//subcategory of page
$data = array(
'subcategory' => 'how_can_we_help',
);
$this->load->model('content_model');
$data['pagecontent'] = $this->content_model->getContent($data);
//...
}