2

Code Igniterにフォームがあり、ヘルパーを使用したいと思いますform_dropdown()。それを行うには、次のような連想配列を準備する必要があります。

$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
          }

質問は次のとおりです:私の場合、連想配列を動的に作成する方法は?

4

1 に答える 1

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'] ] = ...;
    } 
于 2012-08-09T08:20:50.553 に答える