0

当用户点击提交时,我希望将发布数据保存到 cvs 文件中。我想在每个变量之后用“,”将 $valid[$var] 输出到 $cvsData... 我在哪里做这样的事情?我也在看fputcsv这是否会给我同样的结果,把“,”

    $valid = true; 
    foreach($_POST as $var => $value) { 
        if(empty($value)) { 
            $valid[$var] = false;
        } else {
            $valid[$var] = true;
        }
    }

// $cvsData = $phone . “,”。$fn 。“,”。$ln 。“,”。$sos 。“,”。$e1 ."\n";

4

2 回答 2

1
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

$line = "";
foreach($_POST as $value)
{
    if(!empty($value))
    {
        echo '"" . str_replace('"', '\"', $value) . '", ';
    }
}
$line = trim($line, '", ');
echo $line;
于 2012-08-16T12:05:30.190 回答
1

您是否正在寻找这样的东西:

$valid = true;
$csvHeaders=""; 
$csvData="";
foreach($_POST as $var => $value) 
{ 
    if(!empty($value)) 
    {
        $csvHeaders.=$var.",";
        $csvData.=$value.",";
    }
}

echo $csvHeaders;
echo $csvData;
于 2012-08-16T12:03:01.330 回答