Prefill forms from DB array |
[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>
[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.
[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.
[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! >.<
[eluser]Dave Rau[/eluser]
pyro that code is super helpful! Thanks for the post, I'll be using this later today.
[eluser]opel[/eluser]
could this code be made into a helper so it is reusable across all classes?
[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/
|
Welcome Guest, Not a member yet? Register Sign In |