2014-09-26 3 views
0

내 테이블의 모든 밴드에 액세스하여 목록으로 인쇄하기 위해 노력하고 있어요,하지만 난 그것을 실행할 때이 오류가 얻을 :CodeIgniter는 - 정의되지 않은 속성 오류

Severity: Notice 

Message: Undefined property: CI_Loader::$model_bands 

Filename: views/band_view.php 

Line Number: 16 

band_view.php :

<h3>List of Bands:</h3> 
<?php 
$bands = $this->model_bands->getAllBands(); 

echo $bands; 
?> 

model_bands.php : 그것은이 일을 왜

function getAllBands() { 
    $query = $this->db->query('SELECT band_name FROM bands'); 
    return $query->result();  
} 

누군가가 나에게 pleease 수 있을까요? 당신은이 작업을 수행해야 할 이유

답변

2

은 올바른 방법은 다음 뷰에 전달, 컨트롤러 내부 모델 방법을 사용하는 것입니다

public function controller_name() 
{ 
    $data = array(); 
    $this->load->model('Model_bands'); // load the model 
    $bands = $this->model_bands->getAllBands(); // use the method 
    $data['bands'] = $bands; // put it inside a parent array 
    $this->load->view('view_name', $data); // load the gathered data into the view 
} 

다음보기에서 $bands (루프)를 사용합니다.

<h3>List of Bands:</h3> 
<?php foreach($bands as $band): ?> 
    <p><?php echo $band->band_name; ?></p><br/> 
<?php endforeach; ?> 
+0

확실한 foreach ($ bands는 $ band)입니까? 그 가변적 인 밴드가 존재하지 않는다는 이유 때문에 – Divergent

1

컨트롤러에 모델을로드 했습니까?

$this->load->model("model_bands"); 
0

당신은 그런

function getAllBands() { 
    $query = $this->db->query('SELECT band_name FROM bands'); 
    return $query->result();  
} 
+0

당신은 foreach ($ bands를 $ band로) 확신합니까? 가변 밴드가 없다는 말 때문에 – Divergent

0

확인 당신이 모델을로드하는 것을 잊었다 모형은

<h3>List of Bands:</h3> 
<?php foreach($bands as $band){ ?> 
    <p><?php echo $band->band_name; ?></p><br/> 
<?php } ?> 

보기 컨트롤러

public function AllBrands() 
{ 
    $data = array(); 
    $this->load->model('model_bands'); // load the model 
    $bands = $this->model_bands->getAllBands(); // use the method 
    $data['bands'] = $bands; // put it inside a parent array 
    $this->load->view('band_view', $data); // load the gathered data into the view 
} 

을 같이 코드를 변경해야 와이 우리 컨트롤러 :

//controller 

function __construct() 
{ 
    $this->load->model('model_bands'); // load the model 
} 

그런데 왜 당신이보기에서 직접 모델을 호출하고 있습니까? 그래야만합니다 :

//model 
$bands = $this->model_bands->getAllBands(); 
$this->load->view('band_view', array('bands' => $bands)); 

//view 
<h3>List of Bands:</h3> 
<?php echo $bands;?> 
관련 문제