2012-08-09 133 views
2

我在Code Igniter中創建了表單,我想使用幫助器form_dropdown()。爲了做到這一點我要準備這樣的關聯數組:關聯數組 - 動態創建(CodeIgniter)

$options = array(
       'small' => 'Samsung', 
       'med' => 'Apple', 
       'large' => 'HTC', 
       'xlarge' => 'Nokia', 
      ); 

但是在該視圖中該數據從控制器,其從模型取出當然,傳送。

 $this->db->select('id'); 
     $query = $this->db->get('ci_table1'); 

    if ($query->num_rows() > 0) 
    { 
     foreach ($query->result() as $row) 
     { 
      $data[] = $row; 
     }; 
    }; 

    $id_data['id'] = $data; 

    $this->load->view('update_record_view', $id_data); 

因此,對的側面來看我有foreach -loop:

foreach ($id as $row) 
      { 
      // this I want to construct associative array 
      } 

問題是遵循:如何在我的情況下創建關聯數組動態?

+0

您沒有提供足夠的信息來回答這個問題。你的數據來自哪裏?查詢() - > result_array()? – 2012-08-09 08:08:29

+0

是的,你說得對 - 我已經更新了主題。 – 2012-08-09 08:11:59

回答

2

我不明白你的代碼。但也許這就是你要找的。

$this->db->select('id'); 
    $id_data['id'] = $this->db->get('ci_table1')->result_array(); 
    $this->load->view('update_record_view', $id_data);   

和:

$options = array(); 
    foreach ($id as $row) 
    { 
     // this I want to construct associative array 
     $options[ $row['id'] ] = ...; 
    } 
+0

你不明白我的代碼,可能是因爲這是我的第一個CI項目,我使用CI 1.7。但無論如何 - 你編碼的作品,真的我有我需要的數組。非常感謝您的幫助! – 2012-08-09 08:26:47

+0

好的,請將我的答案標記爲解決方案;) – 2012-08-09 08:28:07