Importing CSV
Posted: Thu Mar 02, 2006 4:03 pm
Hello all,
This is the first time I'm trying to do this, and I just can't get it to work. I have a CSV file in this format...
It's in excel, so the color name is in the first column and the file name in second. There are no header rows and no comma's in any field.
The question is how do I use fgetcsv() for each of these values? I need to populate the DB like...
insert into colours values (color_name,color_file), where these are the two fields contained in the CSV.
Here is what I have now, from the PHP manual...
But I don't know how to get the individual columns out. I would assume maybe $data[$c][header], but there is no headers.
Any help is appreciated!
Philip
This is the first time I'm trying to do this, and I just can't get it to work. I have a CSV file in this format...
Code: Select all
Sport_Grey,sport_grey.gif
PoliceBlue,policeblue.gif
Navy_Slate,navy.gifThe question is how do I use fgetcsv() for each of these values? I need to populate the DB like...
insert into colours values (color_name,color_file), where these are the two fields contained in the CSV.
Here is what I have now, from the PHP manual...
Code: Select all
$row = 1;
$handle = fopen("colours.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);Any help is appreciated!
Philip