tags:

views:

184

answers:

3

I have put together a script which will upload a CSV file and then extract the data into an already made table. I want to make it so the first line(the column headers) will not be inserted into the table, but the rest of the data will be.

  $fp = fopen($_SESSION['filename'],"r");


while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)

{
 $import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";

 mysql_query($import) or die(mysql_error());

}

fclose($fp);

this is the part of the code i use to extract the data from the csv file.

Thank You very much for any help with this matter!

+6  A: 

Just put the following before the while loop to read the first line:

fgetcsv($fp, 1000, ",");

Thereafter the while loop starts with the second line instead.

Gumbo
Oh man, you beat me by two seconds...
Scott W
A: 

Underthink it.

Create a boolean flag on the outside, and toggle it once you enter the loop instead of importing, using an if statement.

A: 

Simply do a blank read as such:

$fp = fopen($_SESSION['filename'],"r");
$headerLine = true;

while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)

{
        if($headerLine) { $headerLine = false; }
        else {
                $import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";

                mysql_query($import) or die(mysql_error());
        }

}

fclose($fp);
Andrew Moore