• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a dropdown menu from a database

#1
[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!

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

http://stackoverflow.com/questions/73172...odeigniter

#3
[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!

#4
[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));


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.