使用

2015-05-14 20 views
0

以.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提前

+0

也許是因爲你正在使用'fopen('php:// output','w')'而不是'$ _POST'變量 – Machavity

+0

他正在寫輸出流,這非常好。 –

+0

對不起,我不明白這個想法 – TanGio

回答

2

問題問題就在這裏:

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輸出。

+0

Thx @OndřejHlaváček...這是工作 – TanGio

0

你的問題不是很清楚,但它可能是你想在fclose()後終止腳本的執行。

+0

問題是,在.csv中的數組寫入頁面的源 – TanGio

+0

對不起,也許你可以嘗試改寫它。我不理解。 –

0

它的原因是在代碼中的某個點您可能會回顯或打印某些內容。更好的方法是將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; 
} 
+0

對不起,我不明白你的想法 – TanGio

0

fputcsv($ output,array(''Nom','Prenom'),「;」);

這裏錯字 - 在Nom之前雙單引號。

+0

不工作........ – TanGio