![]() |
populating a form from DB - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: populating a form from DB (/showthread.php?tid=21664) |
populating a form from DB - El Forum - 08-17-2009 [eluser]Unknown[/eluser] Hi, I just started working with CI, and so far i am really enjoying playing around with it. There's one thing that i didn't figure out if it can do or not yet is - Can CI populate forms directly from the DB? (just like it can populate the DB right from the $_POST i expected to find a similar function to populate the form back) Thanks, Roy populating a form from DB - El Forum - 08-17-2009 [eluser]pistolPete[/eluser] Have a look at this thread: http://ellislab.com/forums/viewreply/533725/ populating a form from DB - El Forum - 08-17-2009 [eluser]Unknown[/eluser] i read through the thread and set_value() is just like populating the form my self i don't see the difference between doing value=<?=$fieldname?> and value=<?=set_value('fieldname')?> in both ways i need to go through all my fields and manually set their value and because it's coming from the DB i'll definitely need to convert/manipulate most of the data i figured since there's a way of giving the db class the whole $_POST array and table name, it'll know how to do the opposite, get the same row back and the form and fill it. edit : just for example - Code: <select name="Kids"> That's the only way i found so far to fill in the form's drop downs. Am i missing something? Thanks anyway. Roy populating a form from DB - El Forum - 08-18-2009 [eluser]Skuja[/eluser] i suggest you to use form_dropdown() function from form_helper, it will make your code look much cleaner and there will be no problems with repopulation of dropdownmenu. In your controller: Code: $data['kids_options'] = array(' Code: <?=form_dropdown('Kids', $kids_options, $selected_kid)?> populating a form from DB - El Forum - 08-18-2009 [eluser]Colin Williams[/eluser] I think set_value() is a fairly useless function, especially when using a form view that is either populated with submitted results or populated with default values. $_POST is already a key => value data structure, and what you return from your model is likely a key => value structure as well. Given that, consider this kind of code in an edit function: Code: $form['values'] = get_blog_post_array($id); Then in your view, you access values via $values['key']: Code: <input type="text" value="<?php print form_prep($values['title']) ?>" name="title" /> You can also do some more general sanitizing in your controller if you wish (like form_prep(), etc). |