开发者

Reading CSV in PHP and out put its contents by array value

I want to display the contents of a CS开发者_StackOverflow中文版V file by assigning it to a variable and then display it using echo statement.

This code is not working could someone point the error in it

   $fh = fopen('db.csv', 'r');
   $now = time();
   $data=fgetcsv($fh);
   $data[0]=$name;
   echo $name;

Am a newbie to coding and scripting. Thanks

This is the CSV line that I want to be printed

    katz,26-11-2011,http//www.google.com


Why you first valorize the $data with an array of your CSV and then you overwrite the first position with the $name variable (which apparently is null)?

$data[0] = $name;

should be

$name = $data[0];


You could use the handy fgetcvs function for this.

Might as well post an example for want of a complete answer, shamelessly ripped from that same manpage:

<?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);
}
?>

Hope that fits your needs. Happy coding.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜