
Untitled
By: a guest on
Jul 9th, 2012 | syntax:
PHP | size: 2.23 KB | hits: 30 | expires: Never
public function edit_category($id=NULL)
{
if(!$_POST && !$id)
{
//no post nor id is passed, so redirect to category main page
redirect('category');
}
//check for post data
if($_POST)
{
$this->load->library('form_validation');
$this->form_validation->set_rules('id', 'ID', 'required|integer');
$this->form_validation->set_rules('name', 'Category Name', 'required|xss_clean');
$this->form_validation->set_rules('desc', 'Description of Category', 'required|xss_clean');
if($this->form_validation->run() != FALSE)
{
$query['where'] = array(
'id' => (int)$this->input->post('id')
);
$query['insert_data'] = array(
'name' => $this->input->post('name', TRUE),
'desc' => $this->input->post('desc', TRUE)
);
if($this->category_model->update($query))
{
$this->session->set_flashdata('success', 'category updated');
redirect('category');
}
}
else
{
//validation failed, so get the category details again
//view file will be shown at the end
$data['category'] = $this->category_model->get_by_id((int)$this->input->post('id'));
}
}
//check for ID field
//if it is a posted form, it will not contain the ID, so this step will be skipped
//if the form validation failed, this step will be skipped again
if($id)
{
$id = (int)$id;
if( !$data['category'] = $this->category_model->get_by_id($id))
{
//id exists but the record in db does not exist
$this->session->set_flashdata('error', 'Selected category not found in database');
redirect('category');
}
}
$data['title'] = $this->_set_title('Edit Category');
$data['main_content'] = 'edit_category';
$data['message'] = $this->_set_messages();
$this->load->view('template_admin', $data);
}