CodeIgniter Forums
array to dropdown values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: array to dropdown values (/showthread.php?tid=37467)



array to dropdown values - El Forum - 01-11-2011

[eluser]Mutsop[/eluser]
Hi,

I'm trying to get an array (retrieved from my database) to the values of my dropdown box.
Here is what I have:

controller:
Code:
$this->data['dropdown'] = array(
   $this->my_model->get_values(),
);

And this is my view:
Code:
<?php echo form_dropdown('dropdownname', $dropdown, 'some value');?>

My print_r is:
Quote:Array ( [0] => stdClass Object ( [id] => 1 [name] => value1 [description] => value1description ) [1] => stdClass Object ( [id] => 2 [name] => value2 [description] => value2description ) )

So How do I get the valuexdescription into my dropdown? As this is an array object and should be converted.... Don't really know how Sad Also, when the form should be submitted, the [name] should be passed not the desciption.


array to dropdown values - El Forum - 01-11-2011

[eluser]Atharva[/eluser]
Can't you just return the results from get_values function in this format
Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

which will then serve as second parameter to form_dropdown ?


array to dropdown values - El Forum - 01-11-2011

[eluser]Mutsop[/eluser]
Well technically I can, but would like to know for futur use if possible Big Grin
As for now, it's a simple "privilege" selection box for users. So it shouldn't be dynamicly created.