0
작은 CSV 파일을 구문 분석하고 레코드를 MySQL에 삽입하려고합니다.CSV - 모든 레코드를 업로드하지 않습니다.
난 데 문제는 CSV 150 개 행을 가지고 있다는 것입니다,하지만이 삽입 할 것 :
$file = new SplFileObject($uploadedFile);
$separator = ',';
$rowCounter = 1;
$errors = array();
$fh = fopen($uploadedFile, 'r');
if(!$fh) die('File no good!');
// Get headings
$headings = fgetcsv($fh, 0, ',');
$num_cols = count($headings);
$num_rows = 1;
// Read the file as csv
while ($row = $file->fgetcsv($separator)) {
//missed product if num columns in this row not the same as num headings
if (count($row) != $num_cols) {
$row = array_pad($row, $num_cols, NULL);
}
for ($i=0; $i<count($headings); $i++) {
$raw_prod[$headings[$i]] = $row[$i];
}
$item = new Item();
$item->name = $raw_prod['NAME'];
$item->age = $raw_prod['AGE'];
$item->location = $raw_prod['LOCATION'];
$item->save();
}
I var_dump($item)
나는 모든 150 기록, 만 2 적으로 삽입을 얻을합니다. 이
감사
을해야 이해가 안 돼요 '배열이지만 정의되거나 채워지는 곳을 볼 수 없습니다. –
'$ raw_prod'를 사용하도록 업데이트되었습니다. – user789122