我在 Code Igniter 中有表单,我想使用帮助器 form_dropdown()
。为了做到这一点,我必须准备这样的关联数组:
$options = array(
'small' => 'Samsung',
'med' => 'Apple',
'large' => 'HTC',
'xlarge' => 'Nokia',
);
但在这个 View 中,这些数据是从 Controller 传输的,当然,这些数据是从模型获取的。
$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);
所以,在 View 的一侧,我有 foreach
-loop:
foreach ($id as $row)
{
// this I want to construct associative array
}
问题如下:如何在我的情况下动态创建关联数组?
最佳答案
我不明白你的代码。但也许这就是您正在寻找的。p>
$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'] ] = ...;
}
关于php - 关联数组 - 动态创建 (CodeIgniter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879146/