您好,我有这部分代码:
$request->validate([ 'uploaded_file' => 'required|file|mimes:xls,xlsx' ]); $excel_file = $request->file('uploaded_file'); $file = IOFactory::load($excel_file->getRealPath()); $worksheet = $file->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); $data_detail = []; for ($row = 9; $row < $highestRow; $row++) { $data_detail['codice_articolo'] = $worksheet->getCell("A$row")->getValue(); $data_detail['quantita'] = $worksheet->getCell("B$row")->getValue(); $data_detail['prezzo'] = $worksheet->getCell("C$row")->getValue(); }
Excel
D B:
我正在努力用行填充数据库,因为它们在 excel 文件中,但它只记录第一行
问题在这里:$data_detail['codice_articolo'] =…等:每次运行 for 循环时,这只会覆盖 data_detail 数组中的相同值。您似乎打算改为附加到此数组。
$data_detail['codice_articolo'] =
用这个:
for ($row = 9; $row < $highestRow; $row++) { $newEntry = array(); $newEntry['codice_articolo'] = $worksheet->getCell("A$row")->getValue(); $newEntry['quantita'] = $worksheet->getCell("B$row")->getValue(); $newEntry['prezzo'] = $worksheet->getCell("C$row")->getValue(); $data_detail[] = $newEntry; }
这将创建一个新的关联数组并将其添加到$data_detail. $data_detail然后变成一个条目列表,而不是只包含一个条目。
$data_detail
然后稍后您可以循环$data_detail并将其包含在数据集中以更新数据库,例如:
foreach($data_detail as $i => $row_detail) { $new_orders_details->codice_articolo = $row_detail['codice_articolo']; $new_orders_details->quantita = $row_detail['quantita']; $new_orders_details->prezzo = $row_detail['prezzo']; }