Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 9th, 2012  |  syntax: PHP  |  size: 2.23 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  public function edit_category($id=NULL)
  2.     {
  3.         if(!$_POST && !$id)
  4.         {
  5.                 //no post nor id is passed, so redirect to category main page
  6.             redirect('category');
  7.         }
  8.         //check for post data
  9.         if($_POST)
  10.         {
  11.             $this->load->library('form_validation');
  12.             $this->form_validation->set_rules('id', 'ID', 'required|integer');
  13.             $this->form_validation->set_rules('name', 'Category Name', 'required|xss_clean');
  14.             $this->form_validation->set_rules('desc', 'Description of Category', 'required|xss_clean');
  15.             if($this->form_validation->run() != FALSE)
  16.             {
  17.                 $query['where'] = array(
  18.                     'id' => (int)$this->input->post('id')
  19.                 );
  20.                 $query['insert_data'] = array(
  21.                     'name' => $this->input->post('name', TRUE),
  22.                     'desc' => $this->input->post('desc', TRUE)
  23.                 );
  24.                 if($this->category_model->update($query))
  25.                 {
  26.                     $this->session->set_flashdata('success', 'category updated');
  27.                     redirect('category');
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 //validation failed, so get the category details again
  33.                 //view file will be shown at the end
  34.                 $data['category'] = $this->category_model->get_by_id((int)$this->input->post('id'));
  35.             }
  36.         }
  37.         //check for ID field
  38.         //if it is a posted form, it will not contain the ID, so this step will be skipped
  39.         //if the form validation failed, this step will be skipped again
  40.         if($id)
  41.         {
  42.             $id = (int)$id;
  43.             if( !$data['category'] = $this->category_model->get_by_id($id))
  44.             {
  45.                 //id exists but the record in db does not exist
  46.                 $this->session->set_flashdata('error', 'Selected category not found in database');
  47.                 redirect('category');
  48.             }
  49.         }
  50.         $data['title'] = $this->_set_title('Edit Category');
  51.         $data['main_content'] = 'edit_category';
  52.         $data['message'] = $this->_set_messages();
  53.         $this->load->view('template_admin', $data);
  54.     }