我试图将数据从excel导入我的数据库, 数据库成功插入但错误偏移:6显示。有人能告诉我我的阵列有什么问题吗?
$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');
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);
}
}
答案 0 :(得分:0)
尝试使用您的for loop
:
$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++;
}
实际上你想在array key
和array value
中都使用$ i,并尝试只使用一个增量变量。
当你的循环在最后一次迭代时工作时,它会返回undefined
。
因此,最好为array value
使用另一个增量变量。