2013-11-25 11 views
0

我有一個模型,必須從一個表與公司信息收集信息。 數據如公司名稱,地址等。 因此,我可以使用例如公司名稱來放置頁腳以將該名稱放入發票中。如何從模型中調用控件中的變量?

這是我的模型:

<?php 

class Company extends CI_Model 
{ 
public $name = ''; 
public $adress = ''; 

function __construct() 
{ 
parent::__construct(); 
} 

function get_info() { 
    $query = $this->db->query('SELECT * FROM company'); 

    foreach ($query->result() as $row) { 
     $this->name = $row->name; 
     $this->adress = $row->adress; 
    } 
} 

}

我CONTROLER看起來是這樣的:

public function test() { 
    $this->load->model('Company'); 
    $this->Bedrijf->get_info(); 
    echo $this->bedrijf->name; 

} 

當我打開我的測試網站,我得到一個錯誤:

Undefined property: testproject::$name

有什麼問題我的代碼?

回答

0

嘗試這樣的事情

function get_info() { 
     $query = $this->db->query('SELECT * FROM company'); 

     foreach ($query->result() as $row) { 
      $data['name'] = $row->name; 
      $data['adress'] = $row->adress; 
     } 
     return $data; 
    } 

我CONTROLER看起來是這樣的:

public function test() { 
    $this->load->model('company'); 
    $company_info = $this->company->get_info(); 
    echo $company_info['name']; 

} 
0

因爲你沒有正確加載你的模型。瞭解如何將您的模型加載到控制器中。 http://ellislab.com/codeigniter/user-guide/general/models.html#loading

試試這個。

public function test() { 
    $this->load->model('Company', 'Bedrijf'); 
    $this->Bedrijf->get_info(); 
    echo $this->Bedrijf->name; 

} 


// You are not defining assigning the different object here 
// from that you are calling i.e. $this->Bedrijf->get_info(); 
$this->load->model('Company'); 
$this->Bedrijf->get_info(); 
0

試試這個:

public function test() { 
     $obj_model = $this->load->model('Company'); 
     $Bedrijf->get_info(); 
     echo $Bedrijf->name; 

    } 
0

嘗試這樣的事情

模式

function get_info() { 
    $query = $this->db->query('SELECT * FROM company'); 
    return $query->result(); 
} 

控制器

public function test() { 
    $this->load->model('Company'); 
    $obj = $this->Company->get_info(); 
    echo $obj->name; 
} 
相關問題