Welcome Guest, Not a member yet? Register   Sign In
Prefill forms from DB array
#1

[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)
    {
        $query = $this->db->query("SELECT * FROM $table WHERE $id");
        $result =  $query->row();
    
        var_dump($result);
    
    }

Controller :

Code:
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);
        
        //render
        $this->template->render();
        
    }

View :

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

&lt;?php

  $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;

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

[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:
&lt;input type="text" value="&lt;?=$body['column title']?&gt;"&gt;

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

[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.
#4

[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);

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:

Code:
&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! >.<
#5

[eluser]Dave Rau[/eluser]
pyro that code is super helpful! Thanks for the post, I'll be using this later today.
#6

[eluser]opel[/eluser]
could this code be made into a helper so it is reusable across all classes?
#7

[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/
#8

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

Controller
#9

[eluser]opel[/eluser]
thanks for your help, much appreciated Smile




Theme © iAndrew 2016 - Forum software by © MyBB