我有一個名爲Categories
的數據庫表。我想創建一個包含數據庫中所有類別的表單下拉菜單。我的解決方案是創建一個關聯數組並將其添加到form_dropdown()
函數的第二個參數中。我的結果是一個不需要的多維數組。Codeigniter從數據庫創建關聯數組並在下拉菜單中使用
型號:
function list_categories()
{
$user_id = $this->tank_auth->get_user_id();
$this->db->where('user_id', $user_id);
$this->db->order_by("date_created", "desc");
$query = $this->db->get('categories');
return $query;
}
查看:
//create array
$categories = array();
if ($query->num_rows() > 0)
{
$categories = $query->result_array();
}
//create dropdown
echo form_dropdown('category', $categories, $date_created); //selected value based date created
的代碼給出了一個多維數組
Array (
[0] => Array ([cat_id] => 27 [user_id] => 3 [cat_title] => Some Title [status] => 0 [date_created] => 2012-06-22 18:48:14)
[1] => Array ([cat_id] => 24 [user_id] => 3 [cat_title] => Title [status] => 0 [date_created] => 2012-06-20 19:37:47)
[2] => Array ([cat_id] => 23 [user_id] => 3 [cat_title] => Another Title [status] => 0 [date_created] => 2012-06-20 18:25:45)
etc...
我怎麼能代替上面的Associativ結果e數組,其中ID鍵是類別標識並且值類別標題?
實施例:
$categories = array(
"23" => "some title",
"14" => "another title",
);
謝謝!這很好地工作 – CyberJunkie