-
Notifications
You must be signed in to change notification settings - Fork 0
Add Edit Views
Category:Help::TipsAndTricks | Category:Help::Views
If you have a standard "CRUD" aspect in your web application, you will probably realise that there is a very fine line between adding and editing. If this is the case, it makes sense to have a single View file that will handle the adding and editing of an item - empty fields when adding and fields populated from the database when editing. This allows you to have a single form which you only need to update once.
[h3]How it works[/h3]
When you call the [b]add[/b] or [b]edit/[/b] functions of the controller, the [b]add[/b] view is loaded. If you call [b]edit/[/b] however, the information for the item is retrived from the database and also passed to the view.
The view is configured in such a way that it can handle adding [i]and[/i] editing of information. It achieves this by checking the values of data that is passed to it, and is helped by a function called [b]field()[/b] (listed at the end of this page) which just makes it easier.
Part of the magic is in setting/getting the ID value. The view first checks to see if an ID is present (it will be if you are editing an item). If one is present, the hidden ID field will be set this ID. If, however, no ID is set, the hidden ID field is set to 'X'.
When your form is submitted to save the details, the validation runs, and when successful checks the ID field. If this ID field is X then it calls the model function to insert into the database. Otherwise, it updates the database going by the ID supplied.
Please take a look at the example controller, model and view code below to get a better idea of how this works.
[h3]Controller[/h3]
[b]/controllers/departments.php[/b]
[code] function add(){ $layout['title'] = 'Add Department'; $layout['body'] = $this->load->view('departments/departments_add', NULL, True); $this->load->view('layout', $layout); }
function edit($department_id = NULL){ // Get ID if($department_id == NULL){ $department_id = $this->uri->segment(3); } // Load view // Get department info by ID $body['department'] = $this->department->Get($department_id); $layout['title'] = 'Edit Department'; $layout['body'] = $this->load->view('departments/departments_add', $body, True); $this->load->view('layout', $layout); }
function save(){
// Get ID from form $department_id = $this->input->post('department_id');
// Validation rules $vrules['department_id'] = 'required'; $vrules['name'] = 'required|min_length[1]|max_length[50]'; $this->validation->set_rules($vrules);
// Validation fields $vfields['department_id'] = 'Department ID'; $vfields['name'] = 'Name'; $this->validation->set_fields($vfields);
// Set the error delims to a nice styled red hint under the fields $this->validation->set_error_delimiters('
', '
');if ($this->validation->run() == FALSE){
// Validation failed
if($department_id != "X"){
return $this->edit($department_id);
} else {
return $this->add();
}
} else {
// Validation succeeded!
// Create array for database fields & data
$data = array();
$data['name'] = $this->input->post('name');
$data['description'] = $this->input->post('description');
// Now see if we are editing or adding
if($department_id == 'X'){
// No ID, adding new record
$this->department->Add($data);
} else {
// We have an ID, updating existing record
$this->department->Edit($department_id, $data);
}
// Go back to index
redirect('departments', 'redirect');
}
}[/code]
[h3]Model[/h3]
[b]/models/departments_model.php[/b]
[code]
function Get($id = NULL){
$this->db->select('*');
$this->db->from('departments');
// Check if we're getting one row or all records
if($id != NULL){
// Getting only ONE row
$this->db->where('department_id', $id);
$this->db->limit('1');
$query = $this->db->get();
if( $query->num_rows() == 1 ){
// One row, match!
return $query->row();
} else {
// None
return false;
}
} else {
// Get all
$query = $this->db->get();
if($query->num_rows() > 0){
// Got some rows, return as assoc array
return $query->result();
} else {
// No rows
return false;
}
}
}
function Add($data){ // Run query to insert blank row $this->db->insert('departments', array('department_id' => NULL) ); // Get id of inserted record $id = $this->db->insert_id(); // Now call the edit function to update the actual data for this new row now we have the ID return $this->Edit($id, $data); }
function Edit($id, $data){ $this->db->where('department_id', $id); $result = $this->db->update('departments', $data); // Return if($result){ return $id; } else { return false; } }[/code]
[h3]View[/h3]
[code]<?php if(!isset($department_id)){ $department_id = @field($this->uri->segment(3, NULL), $this->validation->department_id, 'X'); }
echo form_open( 'departments/save', array('class' => 'cssform', 'id' => 'department_add'), array('department_id' => $department_id) ); ?>
Department InformationName <?php $name = @field($this->validation->name, $department->name); echo form_input(array( 'name' => 'name', 'id' => 'name', 'size' => '20', 'maxlength' => '50', 'value' => $name, )); ?>
<?php echo @field($this->validation->name_error) ?>Description <?php $description = @field($this->validation->description, $department->description); echo form_input(array( 'name' => 'description', 'id' => 'description', 'size' => '50', 'maxlength' => '255', 'value' => $description, )); ?>
<?php echo @field($this->validation->description_error) ?>[h3]Field() function - Script[/h3]
[b]/scripts/field.php[/b]:
[code]function field($validation, $database = NULL, $last = ''){ $value = (isset($validation)) ? $validation : ( (isset($database)) ? $database : $last); return $value; }[/code]
Please note that the application/scripts directory functionality has been deprecated. Rather, put the function here: [b]/application/helpers/field_helper.php[/b]
then, in your controller do: [code]$this->load->helper('field');[/code]