[eluser]pabloheim[/eluser]
Hi
first of all , sorry for my english
im having some trouble with the process of editing an item which id is taken from a uri segment. all is fine if you dont commit any mistake. if form validation fails, the page is reloaded but this time without the uri element corresponding to the id.
Here is an example of the situation:
Code:
function edit(){
$this->load->library('form_validation');
$this->form_validation->set_rules('userfile', 'User file', 'callback_check');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->form_validation->run() == FALSE){ // if it fails , load the page again
$data['title'] ='Edit';
$data['page'] ='edit';
$data['id_item'] =$this->uri->segment(3,0); // the id of the item . it is passed to hidden input text
$data['info'] =$this->Item_model->get('file_name',$this->uri->segment(3,0)); // data to be displayed within the form
$this->load->vars($data);
$this->load->view('contenedor');
}
else{ // if ok , insert data through Item_model and then redirect
if($this->Item_model->edit()) {
$this->session->set_flashdata('message', '<div class="success">All is ok! </div>');
redirect('index','location');
}
else echo 'Error'; // problem inserting data
}
}
Ok, so when i enter into the edit page , the url is like:
Code:
localhost/app/index.php/controller/edit/4
where "4" is the id.
If you submit the form and valdation fails, all is reloaded but the url is like:
Code:
localhost/app/index.php/controller/edit
So the id is lost and there is no reference to the element that i wanted to edit.
The first solution was to keep the id in a session, with is code:
Code:
function edit(){
$this->load->library('form_validation');
$this->form_validation->set_rules('userfile', 'User file', 'callback_check');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->form_validation->run() == FALSE){ // if it fails , load the page again
//////MODIFICATION: save the id in session variable
if($this->uri->segment(3,0) != '0') $this->session->set_userdata('id_item',$this->uri->segment(3,0));
//////
$data['title'] ='Edit';
$data['page'] ='edit';
$data['id_item'] = $this->session->userdata('id_item'); //MODIFICATION the id of the item . it is passed to hidden input text
$data['info'] =$this->Item_model->get('file_name',$this->session->userdata('id_item');); // data to be displayed within the form
$this->load->vars($data);
$this->load->view('contenedor');
}
else{ // if ok , insert data through Item_model and then redirect
if($this->Item_model->edit()) {
$this->session->set_flashdata('message', '<div class="success">All is ok! </div>');
$this->session->unset_userdata('id_item'); // MODIFICATION: liberate after use
redirect('index','location');
}
else echo 'Error'; // problem inserting data
}
}
this works, but i think its a little bit complex.
please, can you suggest another way to do this??
Thanks in advance!!