Welcome Guest, Not a member yet? Register   Sign In
Dynamically fill an array from Mysql using Form Helper for dropdown list.
#10

[eluser]DCZ[/eluser]
Hi pasquattro,

My main goal was to fill a dropdown object with the result of a mysql query. CI has a nice library that you can use to query the database, but the problem is that the array that is return is not in the correct shape to send it to a dropdown object.

Therefor I wrote a function to turn the array around.

First I create an empty array

Code:
$result = array();

Then I launch a sql statement using the CI db library and store it into a variable (type : array)

Code:
$array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');

Next I loop thru all the values inside my $array_keys_values array and each result is then put in the variable $row.

Code:
foreach ($array_keys_values->result() as $row)

Now I take my new and empty array called $results. And I will put the right values in the right place. Making my new array $results the perfect object to give to the dropdown function in the view (form_dropdown)

Code:
$result[$row->$key]= $row->$value;


For a better understanding what is going on, print $result and $array_keys_values and you will see the difference.


Messages In This Thread
Dynamically fill an array from Mysql using Form Helper for dropdown list. - by El Forum - 09-01-2009, 01:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB