という名前のデータベーステーブルがありますCategories
。db内のすべてのカテゴリを含むフォームドロップダウンを作成したいと思います。私の解決策は、連想配列を作成し、それをform_dropdown()
関数の2番目のパラメーターに追加することです。私の結果は、不要な多次元配列です。
モデル:
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...
上記の結果を、IDキーがカテゴリIDであり、カテゴリタイトルを評価する連想配列に置き換えるにはどうすればよいですか?
例:
$categories = array(
"23" => "some title",
"14" => "another title",
);