从数据库编码器获取数据的问题


Problems getting data from database codeigniter

创建了我的模型,视图和控制器,试图从我的数据库中获取数据,我收到一个错误,并希望帮助找出为什么我收到此错误:

错误如下:

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;
}
;?>

谁能告诉我我的方法错在哪里?

蝙蝠侠

你在使用模型之前加载了吗?

看起来CI错误的$this参考认为这是他的方法之一,而它指的是一个模型。请确保及时加载模型或在application/config/autolload .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);
   //...
}