Home > front end >  Skipping 2 lines in CSV PHP
Skipping 2 lines in CSV PHP

Time:02-17

I have a code that is skipping 1 line and making a header from it. How can I skip 2 lines ? (One is like it is - header , but second I want to put to the bin) Is there any parameter that control it ?

 if (isset($file) && $file["tmp_name"] != "") {
            $this->dat = $this->ck->getData();

            $fl = fopen($file['tmp_name'], "r");

            // LEAVE A HEADER
            for ($colSkipIdx = 0; $colSkipIdx < $colSkipNum; $colSkipIdx  ) {
                $head = $csv->fgetcsv_reg($fl, 2048, ",");  // 1 LINE THROW AWAY
                mb_language("Polish");
                mb_convert_variables(MAIN_CHARSET, "SJIS-win", $head);
            }
...

CodePudding user response:

You can always use

if ($colSkipIdx == 1) {/*your code*/}

that should be your second line

And if you want to skip it or do something else with all following lines then you can do

if ($colSkipIdx > 1) {/*your code*/}

CodePudding user response:

Normally you try and make this as simple as possible, if the first line contains the column names you just read it and ignore what has been read. Or if you want to make use of the column headings use them as per your needs, so its just as easy to ignore 2 lines the same way


if (isset($file) && $file["tmp_name"] != "") {
    $this->dat = $this->ck->getData();

    $fl = fopen($file['tmp_name'], "r");
    
    $csv->fgetcsv_reg($fl, 2048, ",");  // LINE 1 THROW AWAY
    $csv->fgetcsv_reg($fl, 2048, ",");  // LINE 2 THROW AWAY
    
    // process all the other lines
    while (($line= fgetcsv($fl, 2048, ",")) !== FALSE) {
        //process as normal
    }
  • Related