0

I want to get the data frome the file in PHP.there fore i create code like this

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

but this code give the the output of start to end but i want to start the reading file from some particular number of the row. Is it possible?how to do it??

  • Do you mean you want to start from a particular row in the file, or from a particular column in the row? –  Feb 08 '15 at 06:59
  • possible duplicate of [fgetcsv open at specific row?](http://stackoverflow.com/questions/7162107/fgetcsv-open-at-specific-row) – stefan Feb 08 '15 at 07:03

1 Answers1

0

Call fgets() to read the lines you want to skip before going into the while loop.

<?php
$row = 1;
$skip = 10;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    for ($i = 0; $i < $skip; $i++) {
        fgets($handle);
    }
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612