• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prefill forms from DB array

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 :

function get($table, $id)
        $query = $this->db->query("SELECT * FROM $table WHERE $id");
        $result =  $query->row();

Controller :

function edit($id){
        $data['pagetitle'] = 'Edit';
        //Select data based on ID
        $data['body'] = $this->Pages_model->Get('pages', $id);
        //write out data
        $this->template->write_view('content', 'pages/page_add', $data);

View :

<h1>&lt;?= $pagetitle; ?&gt;</h1>


  $id = $this->uri->segment(3);

if($id == NULL){
  $id = 'X';

$form = array('name'=>'text','rows' => '10', 'cols' => '30');

$attributes = array('class' => 'email', 'id' => 'myform');

echo form_open('pages/form', $attributes); ?&gt;

&lt;?php echo $this->validation->error_string; ?&gt;

         <td><label><div align="right">Title : </div></label></td>
        <td>&lt;?= form_input('title'); ?&gt;</p></td>
             <td><label>Message : </label></td>
            <td colspan="2">&lt;?= form_textarea($form); ?&gt;</p></td>
             <td><label><div align="right">Order : </div></label></td>
            <td>&lt;?= form_input('order'); ?&gt;</p></td>
            <td>&lt;?= form_submit('submit', 'Add Page') ?&gt;</td>
            <td>&lt;?= form_hidden('id', $id) ?&gt;</td>
&lt;? echo form_close(); ?&gt;

Since the data is going into a variable
$data['body'] = $this->Pages_model->Get('pages', $id);

In the view it becomes an array.
&lt;input type="text" value="&lt;?=$body['column title']?&gt;"&gt;

You need to replace column title with the columns in the array.

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:

$this->data->whatever = $this->whatever_model->getWhatever($id);

foreach(array_keys($rules) as $field) {
    if(isset($_POST[$field])) {
        $this->data->whatever->{$field} = $this->validation->{$field};

Then in your views, you can just have:

&lt;?=form_input('order', $whatever->order);?&gt;

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.

could this code be made into a helper so it is reusable across all classes?

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/

[eluser]Phil Sturgeon[/eluser]
If I must!


thanks for your help, much appreciated Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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