2011-10-28 87 views
0

經建立,企圖從我的數據庫獲取數據,我的模型,視圖和控制器我收到一個錯誤,並希望幫助找出爲什麼我收到此錯誤:問題從數據庫中獲取數據笨

的錯誤是如下:

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

我只想檢索數據wher E中的子類= 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; 
} 

;?> 

可能有人好心告訴我的我的方式錯誤。

蝙蝠俠

回答

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); 
    //... 
} 
+0

當然,我沒有.....(aarrggghh ....)*劉海頭在牆上*謝謝達米安! – hairynuggets