0

I'm trying to import data from excel to my database, The database sucessfully inserted but an error offset : 6 show. Can someone tell me what's wrong with my array?

Controller :

$user = $this->input->post('user');
$path = "assets/excel/Book1.xls";

$this->load->library('excel_reader');
$this->excel_reader->read($path);
$data = $this->excel_reader->sheets[0] ;

$dataexcel = Array();

 for ($i = 2; $i <= $data['numRows']; $i++) {
    if($data['cells'][$i][2] == '')
      break;
    $dataexcel[$i-2]['item_id'] = $data['cells'][$i][2];
    $dataexcel[$i-2]['measure_cd_from'] = $data['cells'][$i][3];
    $dataexcel[$i-2]['qty_from'] = $data['cells'][$i][4];
    $dataexcel[$i-2]['measure_cd_to'] = $data['cells'][$i][5];
    $dataexcel[$i-2]['qty_to'] = $data['cells'][$i][6];

}

$this->prodConv->insertConvtData($dataexcel);
$this->load->view('formSukses');

Model :

function insertConvtData($dataarray)
{
for($i=0;$i<count($dataarray);$i++){
       $data = array(
           'item_id'=>$dataarray[$i]['item_id'],
           'measure_cd_from'=>$dataarray[$i]['measure_cd_from'],
           'qty_from'=>$dataarray[$i]['qty_from'],
           'measure_cd_to'=>$dataarray[$i]['measure_cd_to'],
           'qty_to'=>$dataarray[$i]['qty_to']
         );

         $this->db->insert('tb_t_product_convertion',$data);
}
}
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59

1 Answers1

0

Try to use your for loop as like that:

$j = 2; // initialize an integer with 2 for values
for ($i = 0; $i <= $data['numRows']; $i++) {
    if($data['cells'][$i][2] == '')
      break;
    $dataexcel[$i]['item_id'] = $data['cells'][$j][2];
    $dataexcel[$i]['measure_cd_from'] = $data['cells'][$j][3];
    $dataexcel[$i]['qty_from'] = $data['cells'][$j][4];
    $dataexcel[$i]['measure_cd_to'] = $data['cells'][$j][5];
    $dataexcel[$i]['qty_to'] = $data['cells'][$j][6];
    $j++;
}

Actually you want to use $i in both array key and array value and you try to use with only one incremental variable.

When your loop work on last iteration it will return you the undefined.

So, its better to use an another incremental variable for array value.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    thx for the answer, the problem is sloved. The error : my program cant handle null data ini excel, so when i fill all the field in excel file its run with no error – Kadek Aditya Haritama W Jan 08 '16 at 02:52