CodeIgniter Forums

Full Version: Associative Array Argument for form_dropdown()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hello Smile
All of us know that form_dropdown() takes three arguments: a string, an associative array and a boolean value. I wanna ask about the associative array. Take a look at the codes below:
$result = $this->db->query('SELECT `id`, `name` FROM `employee` WHERE `pos_code` IN (6, 7, 8, 9)');
$row = $result->result_array();

We can access the values with $result[0]['id'] etc, right? But there's one thing that's been bugging me: How to make an associative array whose keys are $result[0]['id'], $result[1]['id'], etc and the values are $result[0]['name'], $result[1]['name'] etc?

I'm sorry if the explanation seems confusing.

El Forum

[eluser]C. Jiménez[/eluser]
You can pre-process your result array in model to send expected data to your controller.
public function get_titles ()
$query = = $this->db->query('SELECT `id`, `name` FROM `employee` WHERE `pos_code` IN (6, 7, 8, 9)');
$raw_data =  $query -> result_array ();
foreach ($raw_data as $line){
             $dropdown_array[$line['id']] = $line['name']
return $dropdown_array;