2012-12-06 78 views
-2

我是codeIgniter中的新成員,而且我的數據庫和下拉菜單有點麻煩。dropdown codeigniter

我的模型

function list_kategori(){ 
    $query = $this->db->get($this->table1); 
    $option = array(); 
    foreach($query->result_array() as $row) { 
     $options[$row['id_cat']] =$row['categori']; 
    } 
    return $options; 

} 

我控制器

public function listkategori(){ 
    $data['kategori']=$this->Mtugasuas->list_kategori(); 
    $data ['test'] = form_dropdown('kategori', $data); 

} 

我查看

<?php 
echo form_open('tugasuas/listkategori'); 
echo $test; 
echo form_close(); 
?> 

錯誤

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: test 

Filename: blackcoffe/add.php 

Line Number: 12 

我做錯了什麼? THX求助

回答

0

您需要發送的變量使用此功能查看從控制文件...

$this->load->view('view_file_name',$data); 
0

是不會在所有的工作,至少有一個錯誤超出了Venkat提到,當實際數據在$ data ['kategori']中時,你試圖用$ data填充下拉菜單,這也不是很好的做法,並且會導致極其凌亂的控制器嘗試在控制器(說實話,我甚至沒有積極的,將工作,理論上它應該,但...

您的控制器

public function listkategori(){ 
    $data['kategori']=$this->Mtugasuas->list_kategori(); 
    $this->load->view('viewName',$data); 
    //load the view with $data this way you can pass multiple variables to the same view. 

} 

你查看

<?php 
    echo form_open('tugasuas/listkategori'); 
    echo form_dropdown('kategori', $kategori); 
    echo form_close(); 
?>