-
Notifications
You must be signed in to change notification settings - Fork 26
Add Edit Views
[b]WORK IN PROGRESS !!!![/b]
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]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]