2012-08-09 124 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