Welcome Guest, Not a member yet? Register   Sign In
Best way to populate an edit form from a database
#15

[eluser]Unknown[/eluser]
Here's a very simple solution. I put this in a helper file. In my controller, my "new" function submits the would-be database object "$person" as a null value:

Code:
function new_person()
{
   $data['person'] = null;
   ...
   $this->load->view('person_edit', $data);

}


Whereas it submits the person values from the database if it is the edit request
Code:
function edit_person()
{
   ...
   $data['person'] = $this->person_model->fetch_person( $kPerson );
   ...
   $this->load->view('person_edit', $data);
}

The helper function is this:

Code:
function getValue($object, $item){
    $output = "";
    if($object){
        $output = $object->$item;
    }
    return $output;
}

In the form, then the print out would be something like this:

Code:
...
echo form_input('firstName', getValue($person, 'firstName'));
...

The one catch, which I think could be resolved, is if you type a non-existent property as the "item" parameter for the getValue function. It seems best to have this remain error-producing because you'd want the feedback. If you really wanted to prevent errors from being displayed, you could use:
Code:
$var_list = get_object_vars($object);
$var_keys = array_keys($var_list);
//produces an associative array of object properties
if (in_array($item, $var_keys)){
    $output = $object->$item;
}

I just whipped that one off. It works, but seems inelegant. You could just work with the $var_list array using the larger number of array-manipulation and validation tools available for php to extract the data you want.

You could also add a third function parameter to getValue that allows a default return value


Messages In This Thread
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 09:19 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:14 AM
Best way to populate an edit form from a database - by El Forum - 04-30-2009, 10:25 AM
Best way to populate an edit form from a database - by El Forum - 05-01-2009, 10:01 AM
Best way to populate an edit form from a database - by El Forum - 05-22-2009, 10:15 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 02:02 AM
Best way to populate an edit form from a database - by El Forum - 05-24-2009, 07:19 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:10 PM
Best way to populate an edit form from a database - by El Forum - 01-05-2010, 07:33 PM
Best way to populate an edit form from a database - by El Forum - 01-06-2010, 07:02 AM
Best way to populate an edit form from a database - by El Forum - 01-08-2010, 08:20 AM
Best way to populate an edit form from a database - by El Forum - 02-15-2010, 07:06 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 05:58 AM
Best way to populate an edit form from a database - by El Forum - 03-31-2010, 09:03 AM
Best way to populate an edit form from a database - by El Forum - 03-23-2011, 11:33 AM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:40 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 01:45 PM
Best way to populate an edit form from a database - by El Forum - 07-06-2011, 02:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB