[英]Problems getting data from database codeigniter
Having created my model, view and controller in an attempt to get data from my database I receive an error and would like help finding out why I am receiving this error: 创建我的模型,视图和控制器以尝试从我的数据库中获取数据后,我收到错误,并希望帮助找出我收到此错误的原因:
The error is as follows: 错误如下:
Severity: Notice
Message: Undefined property: How_can_we_help::$Content_model
Filename: controllers/how_can_we_help.php
Line Number: 14
I am trying to get my data by calling a function in my model, controller looks like: 我试图通过调用我的模型中的函数来获取我的数据,控制器看起来像:
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);
}
}
I only want to retrieve data where the subcategory = how_can_we_help 我只想检索子类别= how_can_we_help的数据
Model: 模型:
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;
}
}
}
and finally the view 最后是观点
<?php
foreach ($pagecontent->result() as $row)
{
$title = $row->title;
$intro = $row->intro;
$content = $row->content;
$tags = $row->tags;
}
;?>
Could somebody kindly show me the error of my ways. 有人可以向我展示我的方式的错误。
The Batman 蝙蝠侠
DId you load your model before using it? 你在使用它之前加载你的模型吗?
Looks like CI mistakes the $this
reference thinking it's one of his method, while instead it refers to a model. 看起来CI错误了$this
引用认为它是他的方法之一,而相反它指的是一个模型。 Be sure you load the model in-time or autoload it in application/config/autoload.php: 请确保您及时加载模型或在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);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.