Welcome Guest, Not a member yet? Register   Sign In
faster,better way to get stuff from db + add/edit theory
#5

[eluser]xpix[/eluser]
another thing

how do you manage add and edit controllers into one

Eg
Code:
function addUser(){
  //add user form
  $rules['name'] = "trim|required|max_length[100]|xss_clean";
  $rules['email'] = "trim|required|valid_email";
  $rules['pass'] = "trim|required|min_length[4]|max_length[100]";
  $this->validation->set_rules($rules);
  
  //re-populating
  $fields['name']    = 'Name';
  $fields['email'] = 'Email';
  $fields['pass'] = 'Password';
  $this->validation->set_fields($fields);
  
  if ($this->validation->run() == FALSE){
      //nothing to do - the view is loaded by default                
  }else{
    //get data for db insert
    $user = array(
      'name' => $this->input->post('name'),
      'email' => $this->input->post('email'),
      'pass' => $this->input->post('pass')
    );
    //insert job
    $this->db->insert('users', $user);
  }
}


function editUser($id){
  $data['id'] =  $id;//show links
  
  $query = $this->db->get_where('users',array('id' => $id));
  $result = $query->result();
  //get current fields value to display in view
  $data['oldName'] = $result[0]->name;
  $data['oldEmail'] = $result[0]->email;
  $data['oldPass'] = $result[0]->pass;
  
  //add user form
  $rules['name'] = "trim|required|max_length[100]|xss_clean";
  $rules['email'] = "trim|required|valid_email";
  $rules['pass'] = "trim|required|min_length[4]|max_length[100]";
  $this->validation->set_rules($rules);
  
  //re-populating
  $fields['name']    = 'Name';
  $fields['email'] = 'Email';
  $fields['pass'] = 'Password';
  $this->validation->set_fields($fields);
  
  if ($this->validation->run() == FALSE){
      $this->load->view('editstudent',$data);            
  }else{
    //get data for db insert
    $user = array(
    'name' => $this->input->post('name'),
    'email' => $this->input->post('email'),
    'pass' => $this->input->post('pass')
    );
    //insert job
    $this->db->where('id', $id);
    $this->db->update('users', $user);
    redirect('backend/students');
  }
}

Would you use a function user($action, $id) where $action is "add" or "edit"?

Is there a better way?


Messages In This Thread
faster,better way to get stuff from db + add/edit theory - by El Forum - 07-31-2008, 11:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB