简体   繁体   中英

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

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. Be sure you load the model in-time or autoload it in 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);
   //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM