doushi1996
2011-10-28 15:12
浏览 45
已采纳

从数据库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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlie7962 2011-10-28 15:17
    已采纳

    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);
       //...
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题