Controller file
function import()
{
$filename = $_FILES["file"]["tmp_name"];
if ($_FILES['file']['size'] > 0) {
$file = fopen($filename, "r");
$is_header_removed = FALSE;
while (($importdata = fgetcsv($file, 10000, ",")) !== FALSE) {
if (!$is_header_removed) {
$is_header_removed = TRUE;
continue;
}
$row = array(
'first_name' => !empty($importdata[1]) ? $importdata[1] : '',
'last_name' => !empty($importdata[2]) ? $importdata[2] : '',
'user_name' => !empty($importdata[3]) ? $importdata[3] : '',
'user_password' => !empty($importdata[4]) ? $importdata[4] : '',
'referal_code' => !empty($importdata[5]) ? $importdata[5] : '',
'earnings' => !empty($importdata[6]) ? $importdata[6] : '',
'profile' => !empty($importdata[7]) ? $importdata[7] : ''
);
$this->db->trans_begin();
$this->user_model->add($row);
if (!$this->db->trans_status()) {
$this->db->trans_rollback();
$response['status'] = 'error';
$response['message'] = 'Something went wrong while saving your data';
break;
} else {
$this->db->trans_commit();
$response['status'] = 'success';
$response['message'] = 'Successfully added new record.';
}
}
fclose($file);
}
redirect('user/dashboard');
}
Model File
public function add($row, $dateCreated = FALSE) {
$this->db->insert('user', $row);
return $this->db->insert_id();
}
0 Comments