【发布时间】:2015-07-26 11:59:15
【问题描述】:
我的 .csv 文件有问题。所以我尝试使用 php 从数组中生成 .csv。在我看来:
<form id="form_logistique" action="myroute" method="post">
<div class="form-group " style="float: left;">
<label class="control-label" for="" style="display: inline-block; padding-right: 20px;">Date min</label>
<input type="text" id="date_min" name="date_min.name" value="date_min" placeholder="Date min" class="form-control datepicker" />
</div>
<div class="form-group" style="float: left;padding-left: 20px;">
<label class="control-label" for="" style="display: inline-block; padding-right: 20px;">Date max</label>
<input type="text" id="date_max" name="date_max" value="{{ date_max" placeholder="Date max" class="form-control datepicker" />
</div>
<input type="submit" class="btn btn-primary" style="margin-top: 25px;margin-left: 20px;" value="Rechercher"></input>
<input type="submit" class="btn btn-succes" style="margin-top: 25px;margin-left: 20px;" name="export" value="Exporter"></input>
</form>
在 php 中:
public function getLogistique()
{
$this->form_logistique = new Form\Form(
new Form\Field\Text('date_min', '', true),
new Form\Field\Text('date_max','',true),
);
$date_min = '';
$date_max = '';
if ($this->getRequest()->isPostMethod() && $this->form_logistique->bind($_POST)){
$date_min = $this->form_logistique->date_min->getValue();
$date_max = $this->form_logistique->date_max->getValue();
}
$interdit = array(";", ",", ":", "*", "/", "|", "?", '"', "<", ">", "!", "_", "@", "[", "]", "\\", "{", "}", "~");
$aGifts = Gain::getGiftForLogistique($date_min, $date_max, $statut);
foreach($aGifts as $gift){
$date = explode(' ', $gift['date_gain']);
$gift['ref_article'] = $gift['ref_article'];
$gift['nom'] = str_replace($interdit,"",$gift['nom']);
$gift['prenom'] = str_replace($interdit,"",$gift['prenom']);
$gift['pseudo'] = $gift['pseudo'];
$gift['numero'] = trim(str_replace(";",",",str_replace("\\"," ",$gift['numero'])));
$gift['rue'] = str_replace($interdit,"",$gift['rue']);
$gift['complement'] = str_replace($interdit,"",$gift['complement']);
$gift['code_postal'] = $gift['code_postal'];
$gift['ville'] = str_replace(";",",",str_replace("\\"," ",$gift['ville']));
$gift['pays'] = $gift['pays'];
$gift['email'] = Gain::getEmailByIdm($gift['pseudo']);
$gift['tel'] = str_replace(";",",",str_replace("\\"," ",$gift['tel']));
$gift['id_instant_gagnant'] = $gift['id_instant_gagnant'];
$gift['date_gain'] = $date[0];
$aFilterGifts[] = $gift;
}
$this->aFilterGifts = $aFilterGifts;
if (isset($_POST['export'])) {
$output = fopen('php://output', 'w');
$sFileName = 'Fichier_de_logistique.csv';
header('Content-Disposition: attachement; filename="' . $sFileName . '";');
header('Content-Type: application/download');
fwrite($output, "sep=;\n");
fputcsv($output, array(''Nom', 'Prenom'), ";");
foreach ($aFilterGifts as $value) {
fputcsv($output, $value, ";");
}
fpassthru($output);
fclose($output);
}
return $this->render('template/customer_service/member/logistique.twig');
}
.csv 已生成,但问题是在 .csv 中的数组之后,我拥有页面的所有内容 .html,我不明白问题出在哪里。请帮帮我!提前谢谢
【问题讨论】:
-
可能是因为您使用的是
fopen('php://output', 'w')而不是$_POST变量 -
他正在写入输出流,这很好。
-
对不起,我不明白这个想法
-
您提供的源代码中没有任何其他 html....所以它来自其他地方,我们无法知道。一些 cms 确实会打印页眉/页脚...这可能是正在发生的事情,但如果没有更多关于调用位置的详细信息,就不可能知道,等等...
-
我编辑了这个问题,所以在我的 .csv 内容之后,我得到了 .html 的所有内容
标签: php csv php-5.3 export-to-csv php-5.4