Welcome Guest, Not a member yet? Register   Sign In
Setting Default Values for Add/Edit Forms
#1

[eluser]kirkaracha[/eluser]
Here's how I'm using methods in my controller for setting default values for add and edit. First, the add:

Code:
private function _add_place_defaults(){
    $fields = $this->db->list_fields('places');
    foreach ($fields as $field){
        $values[$field] = NULL;
    }
    return $values;
} // _add_place_defaults

That makes an array with NULL as the value for each field in the database table.

The edit method uses the value from the database if there is one and NULL if there isn't:

Code:
private function _edit_place_defaults($place_info){ // $place_info is the query results row from the model
    foreach($place_info as $key => $value){
        $values[$key] = is_null($value) ? NULL : $value;
    }
    return $values;
} // _edit_place_defaults

Then in the add/edit form view:
Code:
<input name="name" id="name" type="text" value="<?php echo set_value('name',$values['name']); ?>" />

That covers most cases. I also have dates on some forms, which are mm/dd/yyyy fields on the form but stored as DATE in the database, so I add values for day, month and year to the $values array.




Theme © iAndrew 2016 - Forum software by © MyBB