php - Export an array in .csv using -
i have problem .csv. tried generate .csv array using php. in view have :
<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> in 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 $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 $value) { fputcsv($output, $value, ";"); } fpassthru($output); fclose($output); } return $this->render('template/customer_service/member/logistique.twig'); } the .csv generated, problem after array in .csv have content .html of page , don't understand problem.please me! thx in advance
the problem problem lies here:
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 $value) { fputcsv($output, $value, ";"); } fpassthru($output); fclose($output); } return $this->render('template/customer_service/member/logistique.twig'); the function writes headers , content of csv file stdout (php://output), whole circus goes on. function returns content of template it's parent function , renderes stdout (using echo, print or else). easiest thing here (but not correct) put die(); after 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 $value) { fputcsv($output, $value, ";"); } fpassthru($output); fclose($output); die(); } return $this->render('template/customer_service/member/logistique.twig'); the correct way in opinion create new route , controller action csv exports, has no html output.
Comments
Post a Comment