Welcome Guest, Not a member yet? Register   Sign In
Help: Using dropdown form helper with an Array from model
#1

[eluser]rt30000[/eluser]
I have figured out how to do this the manual way (creating my own helper function), but I would really like to use the built in CI forms helper for the dropdown.

Here is a sample of how I would like to use it...
Code:
<?php echo form_dropdown('dessert', $recipes['dessert'], '', $js);?>

The $recipes['dessert'] is an array that contains the 'id' and 'recipe_title' for multiple items. It's my understanding that this should work, I am not sure why it does not. Here is a print_r(); of my $recipes['dessert'] array (two items):
Code:
Array (
[0] => Array ( [id] => 26 [recipe_title] => Angel Food Cake )
[1] => Array ( [id] => 27 [recipe_title] => Chocolate Angel Food Cake )
)

I can loop through the array fine, so why is it not working in the CI form_dropdownCI form_dropdown(); function?

You can view the page HERE.

Thanks so much for shedding any light on this.

Rob
#2

[eluser]Bogdan Tanase[/eluser]
array that is passed to dropdown should look like this:

Code:
$recipes['desert']=array(26=>'Angel Food Cake',
                         27=>'Chocolate Angel Food Cake');

a single dimensional array with key and value.
#3

[eluser]rt30000[/eluser]
I have some reading up to do with php's mysql functions and how to return them. Here is the code I have in my model, how can I return this as a single dimensional array with key and value as you suggest?

Code:
$this->db->select('id, recipe_title');
$this->db->where('category_id', $category_id);
$query = $this->db->get('recipes');
if ($query->num_rows() > 0)
{
     // Return result set as an associative array
     return $query->result_array();
}
#4

[eluser]xwero[/eluser]
Code:
if ($query->num_rows() > 0)
{
     $return = $query->first_row('array');
     for($i = 1;$i<$query->num_rows();$i++)
     {
       $return = array_merge($return,$query->next_row('array'));  
     }
     return $return;
}
Instead of letting a method create the array you create one yourself.

edit : changed code inside of the loop because it was not working.
#5

[eluser]rt30000[/eluser]
I ended up finding an old post after searching for 'single dimensional array', which JUST led me to creating the following solution...
Code:
if ($query->num_rows() > 0)
{
     foreach ($query->result_array() as $item)
     {
    $id = $item['id'];
    $title = $item['recipe_title'];
    $recipes_array[$id] = $title;
     }
     return $recipes_array;
}
You can see the dropdown is working HERE (it just doesnt have the javascript attached yet).

Thanks so much everyone (especially xwero, you seem to help on every post I make!). One more issue resolved! I am beginning to love CI.




Theme © iAndrew 2016 - Forum software by © MyBB