2012-11-05 58 views
1

我是codeIgniter的新手,我的數據庫和下拉菜單有點麻煩。CodeIgniter從查詢中下拉菜單

這裏是我的函數來獲得我需要的信息...

protected $tbl_name = 'club'; 

public function get($id = 0, $object = TRUE) 
    { 
     // select * from users where id = 0 
     // check if id is provided 
     if($id) { 
      // id provided - retrieve a specific user 
      $query = $this->db->get_where($this->tbl_name, array('id' => $id)); 
      // check if object type is requested 
      if($object) { 
       // object is requested 
       $data = $query->row(); 
      } 
      else { 
       // array is requested 
       $data = $query->row_array(); 
      } 
     } 
     else { 
      // id not provided - retrieve all users 
      $query = $this->db->get($this->tbl_name); 
      // check if object type is requested 
      if($object) { 
       // object is requested 
       $data = $query->result(); 
      } 
      else { 
       // array is requested 
       $data = $query->result_array(); 
      } 
     } 
     return $data; 
    } 

這裏就是我稱之爲我的控制器

$data['clubname'] = $this->club_model->get(); 

,這是我認爲的下拉

<tr><td><?php echo form_label('Club Name: ', 'clubname'); ?></td><td><?php echo form_dropdown('clubname', $clubname['name']); ?></td><td><?php echo form_error('clubname'); ?></td></tr> 

但我得到這些錯誤

A PHP Error was encountered 

Severity: Notice 

Message: Undefined index: name 

Filename: individual/individual_club.php 

Line Number: 7 
A PHP Error was encountered 

Severity: Warning 

Message: Invalid argument supplied for foreach() 

Filename: helpers/form_helper.php 

Line Number: 331 

我在做什麼錯?

回答

1

問題在於你的form_dropdown('clubname', $clubname['name'])調用。第二個參數是錯誤的。 form_dropdown需要一個數組。 See the documentaiton

從您的查詢結果中,您需要建立一個俱樂部數組。沿着線的東西:

// array is requested 
$data = array(); 
foreach ($query->result_array() as $row) 
{ 
    $data[$row['club_id']] = $row['club_name']; 
} 

更換club_idclub_name與名稱和俱樂部的ID你的表的列名。之後,將您的form_dropdown更改爲form_dropdown('clubname', $clubname)

像這樣,$clubname是一組俱樂部。

希望這會有所幫助!