[eluser]neen[/eluser]
I have the following save() function in my category controller:
Code:
public function save()
{
// load the validation library
$this->load->library('validation');
// set the validation rules
$rules['name'] = 'required';
$this->validation->set_rules($rules);
// validate the form...
// ...does it validate?
if($this->validation->run() == FALSE)
{
$this->session->set_flashdata('error', 'The category name is required!<br>Please make sure it is filled in.');
redirect('categories/' . $this->session->userdata('return_url'), 'location');
} else {
// save the edited category
// $cat = $this->category_model->save($this->input->post, $this->input->post('id'));
$cat = 1;
$cat = $this->category_model->getCatName($cat);
$return_url = $this->session->userdata('return_url');
// is the ID var set in POST? if it isn't, show the return link, if it is, just show the category list
$this->success = $this->input->post('id') ? "The $cat category was saved!"
: "The $cat category was saved!<br><a >Return</a>";
$this->session->set_flashdata('success', $this->success);
$this->session->set_userdata('redirected', true);
redirect('categories/' . $this->session->userdata('on_save'), 'location');
}
}
and the following is in my category_model:
Code:
public function save($data, $id = NULL)
{
// we are adding a new category
if($id === NULL)
{
// subcat is not set
if($data['subcat'] == 0 || !isset($data['subcat']))
{
// the parent_id of the new category is the value of parentCategory
$data['parent_id'] = $data['parentCategory'];
// subcat is set
} else {
// parent_id is the id of the subcategory
$data['parent_id'] = $data['subcat'];
}
if($data['parent_id'] == 0)
{
// there is no parent_id (this is a top-level category)
$data['parentCategory'] = NULL;
} else {
// get the data for the parent category
$data['parentCategory'] = $this->getCatbyID($data['parent_id']);
}
// remove the parent category name & space from the URL ("naruto/naruto characters" becomes "naruto/characters")
$url = url_title(str_replace($data['parentCategory']['name'] . " ", '', $data['name']), 'dash');
// insert the url
$inserturl = array( 'url' => $url, 'parent_id' => $data['parentCategory']['url_id'] );
$data['url_id'] = $this->url_model->saveURL($inserturl);
// unset the unwanted data (if we leave these in, it will cause an error upon insert)
unset($data['parentCategory'], $data['subcat'], $data['parentCategory_name']);
// insert the category into the categories table
$this->db->insert('categories', $data);
// return the id of the row that was inserted
return $this->db->insert_id();
} else {
// we are updating an existing category
// get the current category
$category = $this->db->get_where('categories', array('id' => $id));
$category = $category->result_array();
$this->db->update('categories', $data);
}
}