function _edit()
{
$this->load->helper(‘security’);
$this->load->library(‘form_validation’);
$data = ‘’;
$id_array = array();
if(!isset($_POST[‘users’])) {
if(isset($_POST[‘select’])) {
$id_array = $_POST[‘select’];
} else {
//$data[‘error_message’][‘select’] = “You must select atleast one user to edit”;
$msg = array(‘error’ => ‘<p>You must select atleast one user to edit.</p>’);
set_global_messages($msg, ‘error’);
unset($_POST);
$this->index();
exit();
}
}
!is_array($id_array) ? $id_array = array() : ‘’;
//START: for the first page load, get data from database
foreach($id_array as $id) {
$id = preg_replace(’/[^0-9]+/’,’‘,$id);
$this->db->where(‘users_id’,$id);
$query = $this->db->get(‘users’);
foreach ($query->result() as $row)
{
$_POST[‘users’][$row->users_id][‘id’] = $row->users_id;
$_POST[‘users’][$row->users_id][‘active’] = $row->active;
$_POST[‘users’][$row->users_id][‘email’] = $row->email;
$_POST[‘users’][$row->users_id][‘firstname’] = $row->firstname;
$_POST[‘users’][$row->users_id][‘lastname’] = $row->lastname;
$_POST[‘users’][$row->users_id][‘groups_id’] = $row->groups_id;
}
}
//END: for the first page load, get data from database
//START: clean data and update in database
if($this->input->post(‘edit’) == ‘Update’ && isset($_POST[‘users’]) && is_array($_POST[‘users’])) {
foreach($_POST[‘users’] as $v) {
//cleaning
$id = (int)preg_replace(’/[^0-9]+/’,’‘,$v[‘id’]); //only intergers
$active = (int)preg_replace(’/[^0-9]+/’,’‘,$v[‘active’]);
$email = xss_clean($v[‘email’]);
$firstname = xss_clean($v[‘firstname’]);
$lastname = xss_clean($v[‘lastname’]);
$groups_id = (int)preg_replace(’/[^0-9]+/’,’‘,$v[‘groups_id’]);
//clean the data to autofill in form
$_POST[‘users’][$id][‘id’] = $id;
$_POST[‘users’][$id][‘active’] = $active;
$_POST[‘users’][$id][‘email’] = $email;
$_POST[‘users’][$id][‘firstname’] = $firstname;
$_POST[‘users’][$id][‘lastname’] = $lastname;
$_POST[‘users’][$id][‘groups_id’] = $groups_id;
//update database if set
if(!empty($email) && !empty($groups_id) && !empty($id)) {
$_POST[‘email’] = $email;
$_POST[‘groups_id’] = $groups_id;
$_POST[‘firstname’] = $firstname;
$_POST[‘lastname’] = $lastname;
$val = array(
array(‘field’ => ‘email’,‘label’ => ‘Email’,‘rules’ => ‘trim|required|xss_clean|valid_email’),
array(‘field’ => ‘groups_id’,‘label’ => ‘Group’,‘rules’ => ‘trim|required|xss_clean’),
array(‘field’ => ‘firstname’,‘label’ => ‘First Name’,‘rules’ => ‘trim|required|xss_clean’),
array(‘field’ => ‘lastname’,‘label’ => ‘Last Name’,‘rules’ => ‘trim|required|xss_clean’)
);
$this->form_validation->set_rules($val);
if ($this->form_validation->run() == FALSE)
{
if(!validation_errors() == ‘’ && $this->input->post(‘edit’) == ‘Update’) {
$msg = array(‘error’ => validation_errors());
set_global_messages($msg, ‘error’);
}
}
else {
$this->db->where(‘users_id’, $id);
$sql_update = array(
‘active’ => $active,
‘email’ => $email,
‘firstname’ => $firstname,
‘lastname’ => $lastname,
‘groups_id’ => $groups_id
);
$this->db->update(‘users’, $sql_update);
$msg = array(‘success’ => ‘<p>Updated successfully.</p>’);
set_global_messages($msg, ‘success’);
}
} else {
$msg = array(‘error’ => ‘<p>Required fields can not be empty!</p>’);
set_global_messages($msg, ‘error’);
}
}
}
//END: validate data and update in database
$assets = array();
//load all required css
//if media type not defined, screen is default.
//$assets[‘css’] = array(‘admin’,‘swiff’,‘box’,‘upload’);
$assets[‘css’] = array(
‘all’ => array(‘admin’,‘users’,‘box’)
);
//load all required js
$assets[‘js’] = array();
$this->assets->load($assets);
//$data[‘users’] = $this->usersmodel->get_users();
//—-
$html_string = $this->load->view(‘users_edit_view’, $data, true);//Get view data in place of sending to browser.
$this->process->view($html_string);
}