Welcome Guest, Not a member yet? Register   Sign In
save and edit share same method?
#3

I use another approach.

Url call for adding new record:
front/add_record

Url call for editing existing record;
front/edit_record/415  
(in this example, I would edit record # 415)

Controller Front.php:
PHP Code:
public function add_record()
{
 
  $this->edit_record(0);
}

public function 
edit_record($id 0)
{
 
  //set form validation rules
 
  if ($this->form_validation->run() == FALSE) {
 
     if ($id 0) {
 
        $record $this->qa_mdl->get_record($id);
 
     }
 
     else {
 
        $record = array(
 
          'id' => 0,
 
          'name' => NULL,
 
          'address' => NULL
         
);
 
     }
 
     $data['record'] = $record;
 
     $this->load->helper('form');
 
     $this->load->view('form_record',$data);
 
  }
 
  else {
 
     $data = array(
 
        'name' => $this->input->post('name'),
 
        'address' => $this->input->post('address')
 
     );
 
     $this->qa_mdl->save_record($data,$id);
 
  }


Model Qa_mdl.php:
PHP Code:
public function get_record($id
{
 
  $query $this->db->get_where('qa_table','id=' .$id);
 
  if ($query->num_rows() > 0) {
 
      return $query->row_array();
 
  }
 
  else {
 
      return NULL;
 
  }
}

public function 
save_record($data,$id)
{
 
   if ($id 0) {
 
     $this->db->update('qa_table',$data,'id='.$id);
 
   }
 
   else {
 
      $this->db->insert('qa_table',$data);
 
   }


View form_record.php :
PHP Code:
<?php
// form validation errors here
extract($record);
echo 
form_open('front/edit_record/' $id);
echo 
'<p>form_input('name',$name) . '</p>';
echo '
<p>form_input('address'$address) . '</p>';
echo 
form_submit('submit''Submit form');
echo 
form_close();
?>
Reply


Messages In This Thread
save and edit share same method? - by iridion2015 - 05-05-2016, 08:40 PM
RE: save and edit share same method? - by Wouter60 - 05-06-2016, 11:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB