I have a database table named Categories. I want to create a form dropdown containing all the categories in the db. My solution is to create an associative array and add it in the second parameter of the form_dropdown() function. My result is an unwanted multidimensional array.
Model:
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;
}
View:
//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
The code gives a multidimensional array
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...
How can I replace the result above with an Associative Array where the ID key is the category id and value the category title?
Example:
$categories = array(
"23" => "some title",
"14" => "another title",
);