![]() |
Prefill forms from DB array - 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: Prefill forms from DB array (/showthread.php?tid=11922) |
Prefill forms from DB array - El Forum - 09-28-2008 [eluser]opel[/eluser] Please could anyone tell me if it is possible or how to prefill a form view with data from my model and controller ? What I have at the moment is : Model : Code: function get($table, $id) Controller : Code: function edit($id){ View : Code: <h1><?= $pagetitle; ?></h1> Prefill forms from DB array - El Forum - 09-28-2008 [eluser]LifeSteala[/eluser] Since the data is going into a variable Code: $data['body'] = $this->Pages_model->Get('pages', $id); In the view it becomes an array. Code: <input type="text" value="<?=$body['column title']?>"> You need to replace column title with the columns in the array. Prefill forms from DB array - El Forum - 09-29-2008 [eluser]opel[/eluser] Thanks I was hoping the Forms helper may have something in it for setting the defaults. I used to use HTML Quickforms in PEAR and you could just do $form->set defaults and it would prefill input fields if the array and form names matched up. Prefill forms from DB array - El Forum - 09-30-2008 [eluser]Phil Sturgeon[/eluser] CI has nothing like this built in but you can hack the same sort of effect. in your controller after your validation rules have been set enter: Code: $this->data->whatever = $this->whatever_model->getWhatever($id); Then in your views, you can just have: Code: <?=form_input('order', $whatever->order);?> It might look a little crazy, but it works very well. You are basically getting the data from the db, then unless the POST variable has a more up to date version, you are putting the DB values into the form. Would be nice to have a way of doing this a little easier, but I think getting too involved in CRUD automation is a very bad idea. Every system I have ever used to do this has made my life harder, more stressful and less productive! >.< Prefill forms from DB array - El Forum - 09-30-2008 [eluser]Dave Rau[/eluser] pyro that code is super helpful! Thanks for the post, I'll be using this later today. Prefill forms from DB array - El Forum - 09-30-2008 [eluser]opel[/eluser] could this code be made into a helper so it is reusable across all classes? Prefill forms from DB array - El Forum - 10-01-2008 [eluser]opel[/eluser] Pyro do you have an example of your controller or views that you could post up please ? I have been trying to follow this in the wiki but getting nowhere : http://codeigniter.com/wiki/Add_Edit_Views/ Prefill forms from DB array - El Forum - 10-01-2008 [eluser]Phil Sturgeon[/eluser] If I must! Controller Prefill forms from DB array - El Forum - 10-01-2008 [eluser]opel[/eluser] thanks for your help, much appreciated ![]() |