Re: Admin multi edit feature validation

For a quick solution you can replace edit function with this one:


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);
}

continue reading ...

 

 

 

Author dbashyal has posted total 30 posts.
click here To post comments on the above post.
Date: 08-05-2010 17:58:38pm

 

 

You may also like:

And, Around the globe:

 

 

 

 1 2 3 4 >  Last ›
 


Page rendered in 2.0768 seconds

 
Powered by: Codefight CMS | A Damodar Bashyal Creation.