CodeIgniter Forums
Associative Array Argument for form_dropdown() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Associative Array Argument for form_dropdown() (/thread-43415.html)



Associative Array Argument for form_dropdown() - El Forum - 07-10-2011

[eluser]kaege[/eluser]
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:
Code:
$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.


Associative Array Argument for form_dropdown() - El Forum - 07-11-2011

[eluser]C. Jiménez[/eluser]
You can pre-process your result array in model to send expected data to your controller.
Code:
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;
}