使用

以.csv格式导出数组我的.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不明白问题在哪里,请帮助我! Thx提前

回答:

问题问题就在这里:

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文件输出到标准输出(php://output)的内容,但那么整个马戏团的推移。这个函数将模板的内容返回给它的父函数,这可能会将其渲染为STDOUT(使用echo,print或其他)。最简单的事情在这里(但不正确的),将是把die();fclose($output);

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

die();

}

return $this->render('template/customer_service/member/logistique.twig');

在我看来,正确的方法是创建CSV出口的新路线和控制器动作,有没有HTML输出。

回答:

你的问题不是很清楚,但它可能是你想在fclose()后终止脚本的执行。

回答:

它的原因是在代码中的某个点您可能会回显或打印某些内容。更好的方法是将csv写入文件。然后将该文件作为Content-Disposition:附件发送。就像在下面的代码中,file是文件的路径一样。

if (file_exists($file)) { 

header('Content-Description: File Transfer');

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename='.basename($file));

header('Expires: 0');

header('Cache-Control: must-revalidate');

header('Pragma: public');

header('Content-Length: ' . filesize($file));

readfile($file);

exit;

}

回答:

fputcsv($ output,array(''Nom','Prenom'),“;”);

这里错字 - 在Nom之前双单引号。

以上是 使用 的全部内容, 来源链接: utcz.com/qa/266167.html

回到顶部