I want to insert data from Excel with codeigniter, but I found something difficult for me. When I upload file, there is a some data double in primary key. I try to check it first before upload with
Code:
$cek=$this->m_admin->get_redudant($data['cells'][$i][2]);
if ($cek==0) {
$this->m_admin->add($dataexcel);
redirect('home','refresh');
}
else{
echo "something wrong";
}
Here is function upload file:
Code:
function do_upload(){
$config['upload_path'] = './temp_upload/';
$config['allowed_types'] = 'xls';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data = array('error' => $this->upload->display_errors());
}
else
{
$data = array('error' => false);
$upload_data = $this->upload->data();
$this->load->library('excel_reader');
$this->excel_reader->setOutputEncoding('CP1251');
$file = $upload_data['full_path'];
$this->excel_reader->read($file);
error_reporting(E_ALL ^ E_NOTICE);
$data = $this->excel_reader->sheets[0] ;
$dataexcel = Array();
$i=0;
for ($i = 1; $i <= $data['numRows']; $i++) {
if($data['cells'][$i][1] == '') break;
$dataexcel[$i-1]['nip'] = $data['cells'][$i][2];
$dataexcel[$i-1]['nama'] = $data['cells'][$i][1];
$dataexcel[$i-1]['nipbr'] = $data['cells'][$i][3];
$dataexcel[$i-1]['tempat'] = $data['cells'][$i][4];
$dataexcel[$i-1]['lahir'] = $data['cells'][$i][5];
$dataexcel[$i-1]['temp_kerja'] = $data['cells'][$i][6];
}
delete_files($upload_data['file_path']);
$cek=$this->m_admin->get_redudant($data['cells'][$i][2]);
if ($cek==0) {
$this->m_admin->add($dataexcel);
redirect('home','refresh');
}
else{
echo "something wrong";
}
}
}
I want to manipulate data in primary key. If data is exist , I will add data random in the end of id like
Code:
else{
$x = rand(1, 1000);
$dataexcel[$i-1]['nip'] = $data['cells'][$i][2]."$x";
}
but it's not work , what should I do..