Welcome Guest, Not a member yet? Register   Sign In
Associative Array Argument for form_dropdown()
#1

[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.
#2

[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;
}




Theme © iAndrew 2016 - Forum software by © MyBB