![]() |
Populate drop down list from 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: Populate drop down list from database (/showthread.php?tid=33626) |
Populate drop down list from database - El Forum - 09-02-2010 [eluser]adaxa[/eluser] I`m using this code to repopulate drop down list from the database : Code: $city_id = 15; It works like a charm but which is the best practice in codeigniter ? Populate drop down list from database - El Forum - 09-02-2010 [eluser]davidbehler[/eluser] I usually do something like this: Controller: Code: $data['city_list'] = $this->City_model->get_dropdown_list(); Model: Code: function get_dropdown_list() View: Code: <?php echo form_dropdown('city_id', $city_list, set_value('city_id', $city_id)); Totally untested and especially the get_dropdown_list function can be improved a lot, but I think you get what I'm doing here. Populate drop down list from database - El Forum - 09-02-2010 [eluser]adaxa[/eluser] I`m using almost same code as yours. My question is about how to populate drop down list in edit page. For example a user is registered with selected city lets say New York. When he goes to his edit profile page there is a form and a drop box list with New York selected. This is what i`m trying to handle. Populate drop down list from database - El Forum - 09-02-2010 [eluser]adaxa[/eluser] I create helper function which is working well Code: if ( ! function_exists('drop_down')) In the view Code: echo drop_down('mylist', 3, $data); Where mylist is the name of select element. 3 is the number which will be matched against $data and $data is associative array containing id and value of option. Any comments ? Populate drop down list from database - El Forum - 09-02-2010 [eluser]davidbehler[/eluser] Why not use the form_dropdown() function CI provides out of the box? Populate drop down list from database - El Forum - 09-02-2010 [eluser]adaxa[/eluser] Because I should read user guide carefully.... Thanks Populate drop down list from database - El Forum - 09-02-2010 [eluser]Kamarg[/eluser] I often times will have need of multiple fields from a table for one use and two specific fields from the same result set for a select so I wrote the below function. Code: /** Using this I can do a select * on my table and still easily build an array for my select without having to write a foreach or a new function every time. |