-
Notifications
You must be signed in to change notification settings - Fork 0
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 Functions [/h3]
[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 Functions [/h3]
[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]