通过PHP从CSV获取数据

I am having one csv file with three columns and Name , month and expanses. The file name is : name.csv. The file format is like this.

NAME    MONTH   EXPANSES
JHON    JAN     200
GATES   JAN     150
JHON    MAR     150
BILL    APP     100

Whereas I am having one php file: name.php which will fetch the required data from the csv file and the sours code is:

<?php
$x="JHON"; // to be checked in the first column
  $handle = fopen("name.csv","r")or die("file dont exist");
  $output = '';
   while (!feof($handle )){
    $data = fgetcsv($handle,4096,",");
        if(($data[0] ==$x)){
           $output .= sprintf( "Month: %s     Expanses: %d)<br>", 
           $data[1], $data[2]);
        }
    }
echo $output;
fclose($handle);
?>

I want to sort out where the name is “JHON”, with the above code and the result is as following.

Month:  JAN            Expanses:  200 
Month:  MAR           Expanses:  150 
Month:  AUG           Expanses:  250

But what I want is heading like: Expanses of JHON and at bottom, total expenses: amount

MONTH WISE EXPANSES OF JHON 
Month:  JAN            Expanses:  200 
Month:  MAR           Expanses:  150 
Month:  AUG           Expanses:  250

TOTAL EXPANCES:   600

Can anybody help me in correcting the php code please?

dongshao1873
dongshao1873 我的问题与您提到的问题不同。
4 年多之前 回复
dqbr37828
dqbr37828 可能重复如何从PHP中的csv文件中提取数据
4 年多之前 回复

2个回答

Actually you already exctract data from your CSV. As far I understand it you only want to add a header at the top and a sum at the bottom.

Why not just do something like this?

<?php
    $x="JHON"; // to be checked in the first column
    $handle = fopen("name.csv","r") or die("file doesn't exist");
    $output = '';
    $total = 0;

    while (!feof($handle )){
        $data = fgetcsv($handle, 4096, ",");
        if(($data[0] == $x)){
            $output .= sprintf( "Month: %s     Expenses: %d)<br>", $data[1], $data[2]);

            $total += $data[2];
        }
    }

    fclose($handle);

    echo "MONTHLY EXPENSES OF " . $x . "<br/><br/>";
    echo $output . "<br/><br/>";
    echo "TOTAL EXPENSES:   " . $total;
?>

Remark 2: It's "expense", "expance" is not a word

douyan8961
douyan8961 当然,纠正了我的错字。 如果有帮助,请标记为已回答。
4 年多之前 回复

use following function

function fetchCSV($filename, $header = true, $delimiter = ',') 
{
    if (!file_exists($filename) || !is_readable($filename)) 
    {
        return FALSE;
    }
    $data = array();
    $header_data = array();

    if (($handle = fopen($filename, 'r')) !== FALSE) 
    {
        $r = 0;
        while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) 
        {
            if ($header && $r == 0) 
            {
                $header_data = $row;               
            } 
            else 
            {
                if ($header)
                {
                    for($i = 0; $i < count ($row); $i++)
                    {
                        $data[$r][$header_data[$i]] = $row[$i];
                    }
                }
                else
                {
                    $data[$r] = $row;
                }
            }
            $r++;
        }
        fclose($handle);
    }
    return $data;
}

$data = fetchCSV("file.csv", true);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐