Asked  7 Months ago    Answers:  5   Viewed   36 times

I am trying to import a CSV file. Due to the program we use, the first row is basically all headers that I would like to skip since I've already put my own headers in via HTML. How can I get the code to skip the first row of the CSV? (the strpos command is to cut off the first field in all the rows.)

<?php
$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
           $row++;
    for ($c=0; $c < $num; $c++) {
    if(strpos($data[$c], 'Finished') !== false) {
    $c++;
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
    Else{
        echo "<TD nowrap>" .  $data[$c] . "</ TD>";
        }
    }
}
fclose($handle);
}
?>

 Answers

58

As you are keeping track of the row number anyway, you can use continue to skip the rest of the loop for the first row.

For example, add this at the start of your while loop (just above $num = count($data)):

if($row == 1){ $row++; continue; }

There are other ways to do this, but just make sure that when you continue, $row is still being incremented or you'll get an infinite loop!

Wednesday, March 31, 2021
 
alioygur
answered 7 Months ago
61

OK, solved.

This is what everyone was suspecting: the encoding of the file was messed up. I could not know which encoding this was, but LibreOffice proposed me Unicode whenever I tried to open the CSVs.

I had to open them with nano to realize there was indeed an encoding problem. Gedit, vim or any other tool I had on my computer raised no errors. When opened with nano, an @ symbol was inserted between every other characters and line feeds were not read correctly.

It seems there are some encodings that are not well supported by fgetcsv. To solve the problem, I recreated the files from nano (copy-paste from another tool that did not display the @).

Saturday, May 29, 2021
 
jcubic
answered 5 Months ago
43

This is specific to the Mac version of Excel. Line breaks are handled differently on Macs for some arbitrary reason, so before using fgetcsv, you should do this;

ini_set('auto_detect_line_endings',TRUE);
Thursday, July 29, 2021
 
diegoiglesias
answered 3 Months ago
17

You can delete the first line of a file using sed:

sed -i '' 1d file.csv

If you just want to get the contents of the file without the first line, and without modifying the file, remove the -i '' flag:

sed 1d file.csv

Here 1d is the command to execute:

  • 1 => line where to act
  • d => delete

So 1d means 'delete line 1'

If you want to get the first line, you can use sed too:

sed -n 1p file.csv

Here p stands for 'print' or

sed 1q file.csv

(see William Pursell's comment)

Sunday, August 8, 2021
 
Sudeepta
answered 3 Months ago
53

Building on Nouveau's code you could do this

for($i = 0; $i <= count($fieldList); $i++)
{
    if (is_numeric($fieldList[$i]))
    {
        if (strpos($fieldList[$i],'.') !== false){
            $fieldList[$i] = (int)$fieldList[$i];
        }else{
            $fieldList[$i] = (float)$fieldList[$i];
        }
    }

    switch(gettype($fieldList[$i])) {
        case 'integer':
            $typeInfo = 'int(11)';
            break;
        case 'float':
        case 'double':
            $typeInfo = 'float';
            break;

        case 'string':
            $typeInfo = 'varchar(80)';
            break;
        default:
            $typeInfo = 'varchar(80)';
            break;
    }
if(gettype($fieldList[$i]) != NULL) echo "t".'`'.$i.'` '.$typeInfo.' NOT NULL, --'.gettype($fieldList[$i]).' '.$fieldList[$i]."<br/>n";

}

That works, note the addition of "case 'double':" in the switch, but there may be a better way to do the int/float check as they would only work with standard uk/us numbers.

Friday, August 13, 2021
 
maelgrove
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :