Reputation: 429
CSV format
"Type","Stock","VIN","Year"
"Used","5262G","1G1YY12S435101008","2003"
"Used","5539G","YV4902DZ2D2439476","2013"
"Used","15460","YV4612HK3F1000423","2015"
My code to get the TYPE, STOCK, VIN, YEAR
$file = fopen("files/stillmanmvolvo.csv", "r");
echo "</br></br>";
while(! feof($file))
{
$file_data = fgetcsv($file);
$type = $file_data[0];
$stock = $file_data[1];
$vin = $file_data[2];
$year = $file_data[3];
echo $type .' '. $stock .' '. $vin .' '. $year . '<br><br>';
}
fclose($file);
Output
Type Stock VIN Year
Used 5262G 1G1YY12S435101008 2003
Used 5539G YV4902DZ2D2439476 2013
Used 15460 YV4612HK3F1000423 2015
With the above code I am getting the data, but the data includes the first ROW
ie, "Type","Stock","VIN","Year"
(please see first line in the output)
I want to exclude
the first row, what changes in my code need to do so that it exclude the first ROW..?
Thank you
Upvotes: 1
Views: 1241
Reputation: 40639
Try this,
$file_data = fgetcsv($file);// now the file pointer is on second row
while(! feof($file))
{
$file_data = fgetcsv($file);
$type = $file_data[0];
$stock = $file_data[1];
$vin = $file_data[2];
$year = $file_data[3];
echo $type .' '. $stock .' '. $vin .' '. $year . '<br><br>';
}
Upvotes: 1
Reputation: 10479
Try just reading the line into empty space to skip it :
$file = fopen("files/stillmanmvolvo.csv", "r");
echo "</br></br>";
// read first line
if(!feof($file)) { fgetcsv($file); }
// continue reading the rest of the file
while(! feof($file))
{
$file_data = fgetcsv($file);
$type = $file_data[0];
$stock = $file_data[1];
$vin = $file_data[2];
$year = $file_data[3];
echo $type .' '. $stock .' '. $vin .' '. $year . '<br><br>';
}
fclose($file);
Upvotes: 1
Reputation: 1943
Just call fgets($file)
after fopen()
but before the while
loop
$file = fopen("files/stillmanmvolvo.csv", "r");
fgets($file)
echo "</br></br>";
while(! feof($file))
{
$file_data = fgetcsv($file);
$type = $file_data[0];
$stock = $file_data[1];
$vin = $file_data[2];
$year = $file_data[3];
echo $type .' '. $stock .' '. $vin .' '. $year . '<br><br>';
}
fclose($file);
Upvotes: 1
Reputation: 5512
Try this:
$file = fopen("files/stillmanmvolvo.csv", "r");
$first = false;
echo "</br></br>";
while(! feof($file))
{
$file_data = fgetcsv($file);
if (!$first) {
$first = true;
continue;
}
$type = $file_data[0];
$stock = $file_data[1];
$vin = $file_data[2];
$year = $file_data[3];
echo $type .' '. $stock .' '. $vin .' '. $year . '<br><br>';
}
fclose($file);
Upvotes: 0