开发者

Display specific fields from a CSV file

I'm very new to PHP. I have looked around at other questions but none of them seem to provide a solution, so hopefully someone can help!

I have a csv file, but wish to pick out individual fields instead of displaying a whole column.

Is this possible with php?

My code so far (below) picks out specific columns which is not quite what I want to do. If it could pick out specific rows, that would be better than what it's currently showing, but ideally I'd be able to pick specific fields out.

             <table>
                <?php
                    $handle = fopen("test.开发者_StackOverflow中文版csv", "r");
                    while (!feof($handle) ) {
                        $line_of_text = fgetcsv($handle, 1024, ",");
                        print "<tr><td>" . $line_of_text[0] . "</td><td>" . $line_of_text[5] . "</td></tr>";
                    }
                    fclose($handle);
                ?>
             </table>

Hopefully that makes sense!


fgetcsv() only reads the file line by line, so if you want to skip to a particular line, you'd have to put that in yourself:

$desired_line = 17;
$current_line = 0;

while($line = fgetcsv($handle)) {
   $current_line++;
   if ($current_line < $desired_line) {
      continue; // keep reading more lines until we reach 17.
   }
   print blah blah blah;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜