CodeIgniter Forums
Making a dropdown menu from a database - 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: Making a dropdown menu from a database (/showthread.php?tid=52779)



Making a dropdown menu from a database - El Forum - 06-26-2012

[eluser]reghan[/eluser]
Hello I am interested in making a dropdown menu which pulls info from the database.
Basically I have three possible anwers yes, no, none. I want to pull the answer that is stored in the database and have that value selected in the dropdown. I am new to code igniter and I am not sure how to do this.

Any feedback would be great!

Thanks!


Making a dropdown menu from a database - El Forum - 06-26-2012

[eluser]Jason Hamilton-Mascioli[/eluser]
This is a good start. They outline the controller and model setup.

http://stackoverflow.com/questions/7317203/how-to-populate-dropdownlist-from-database-in-codeigniter


Making a dropdown menu from a database - El Forum - 06-26-2012

[eluser]reghan[/eluser]
Thanks for your reply.

I think my main question is how do I select a value in the drop down which corresponds with the value being stored in the database. There would be values already in the database and then when a user goes to update there would be multiple dropdowns for each field with the value selected.

Thanks again!


Making a dropdown menu from a database - El Forum - 06-26-2012

[eluser]CroNiX[/eluser]
Code:
$data = $this->db->select('id, name')->get('users')->result_array();

$choices[0] = 'Select a User';  //Set a default value.  Don't use 0 if you have an ID of 0 in the db.

//array that form_dropdown() needs has to be in format of (id => name)
//where id is the value that will get sent with the form and name is what is shown in the text of the option
foreach($data as $d)
{
  $choices[$d['id']] = $d['name'];
}

echo form_dropdown('users', $choices, set_value('users', 0));