Search code examples
phpwhile-loopechobuffering

PHP echo issue while in a while loop


I read in a csv file by using a while loop:

while (($data = fgetcsv($handle, null, ",")) !== FALSE) 

and i want to skip the first row because this is the title row and i want to display on the screen "first line skipped".

if($data[0]=="title")
echo "Title row..skipping<br />";  
else
//do stuff

The problem is since its in a while loop it prints out "Title row...skipping" a bunch of times shown here:

Checking row 0...
Title row..skipping
Title row..skipping
Title row..skipping
Title row..skipping
Title row..skipping
Title row..skipping
Title row..skipping
Checking row 1...

what should i do so it only prints it out once? does it have something to do with php's output buffering?


Solution

  • if($data[0]=="title") {
      if (!$skipped) {
        echo "Title row..skipping<br />";
        $skipped = true;
      }
    }
    else
    //do stuff