[eluser]digity[/eluser]
I'm trying to code a feature that allow users to update their profile, but I can't figure out how to properly set up the function in the controller. As you can see from the code below the model (update_user() in models/user_model.php) is already set up but I'm stumped as to what to do in the controller function (update() in controllers/user.php) - specifically how to process ONLY the changed/updated fields. Any ideas?
update_user() in models/user_model.php
Code:
function update_user($options = array()) {
// required values
if(!$this->_required(array('user_id'), $options))
return false;
if(isset($options['email']))
$this->db->set('email', $options['email']);
if(isset($options['username']))
$this->db->set('username', $options['username']);
if(isset($options['password']))
$this->db->set('password', md5($options['password']));
if(isset($options['user_first_name']))
$this->db->set('user_first_name', $options['user_first_name']);
if(isset($options['user_last_name']))
$this->db->set('user_last_name', $options['user_last_name']);
if(isset($options['user_birthdate']))
$this->db->set('user_birthdate', $options['user_birthdate']);
if(isset($options['user_hometown']))
$this->db->set('user_hometown', $options['user_hometown']);
if(isset($options['user_blurb']))
$this->db->set('user_blurb', $options['user_blurb']);
if(isset($options['user_status']))
$this->db->set('user_status', $options['user_status']);
$this->db->where('user_id', $options['user_id']);
$this->db->update('users', $options);
return $this->db->affected_rows();
}
update() in controllers/user.php
Code:
function update() {
if (isset($this->input->post('email'))
$this->form_validation->set_rules('email','email address','trim|required|valid_email|min_length[6]|max_length[50]');
if (isset($this->input->post('username'))
$this->form_validation->set_rules('username','username','trim|required|min_length[3]|max_length[15]');
if (isset($this->input->post('password')) {
$this->form_validation->set_rules('password_current','current password','trim|required|min_length[6]|max_length[32]');
$this->form_validation->set_rules('password','new password','trim|required|min_length[6]|max_length[32]');
$this->form_validation->set_rules('password_confirm','new password confirm','trim|required|matches[password]');
}
if (isset($this->input->post('user_first_name'))
$this->form_validation->set_rules('user_first_name','full name','trim|min_length[2]|max_length[25]');
if (isset($this->input->post('user_last_name'))
$this->form_validation->set_rules('user_last_name','username','trim|required|min_length[3]|max_length[15]');
if (isset($this->input->post('user_birthdate'))
$this->form_validation->set_rules('user_birthdate','username','trim|required|min_length[3]|max_length[15]');
if (isset($this->input->post('user_hometown'))
$this->form_validation->set_rules('user_hometown','username','trim|required|min_length[3]|max_length[15]');
if (isset($this->input->post('user_blurb'))
$this->form_validation->set_rules('user_blurb','blurb','trim|min_length[2]|max_length[140]');
if($this->form_validation->run()) {
// S T U M P E D!!! S T U M P E D!!! S T U M P E D!!!
// S T U M P E D!!! S T U M P E D!!! S T U M P E D!!!
// S T U M P E D!!! S T U M P E D!!! S T U M P E D!!!
// S T U M P E D!!! S T U M P E D!!! S T U M P E D!!!
// S T U M P E D!!! S T U M P E D!!! S T U M P E D!!!
}
$this->template->set_template('default');
$this->template->write_view('content', 'user/create_user');
$this->template->write('_page_title', 'create an account');
$this->template->render();
}
Any kind of help would be appreciated!
Thanks in advance!