2017-04-01 7 views
0

私はデータベースからデータを取得し、そのデータをcsvファイルにダウンロードしています。機能は期待どおりに動作していますが、ダウンロードされているCSVファイルにもページソースコードが含まれていますが、なぜこれが起こっているのか分かりませんし、長い間試しています。誰かが私にこれを手伝ってくれることを願っているはpdoを使用してcsvにエクスポートできません

public function exportSalesCsv() 
{ 

$conn = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password')); 
$sql = "SELECT 
      CONCAT(u.name, ' ', u.surname) AS salesPerson, 
      t.initiation_timestamp AS saleDate, 
      t.customer_account_id AS customerAccount, 
      tt.transaction_type AS transactionType, 
      r.resource_unique_value AS soldResource, 
      CONCAT(rb.resource_brand, 
        ' ', 
        rm.resource_model, 
        ' ', 
        rt.resource_type) AS resourceModel, 
      r.voucher_value_id as value 
     FROM 
      ims.transactions t 
       INNER JOIN 
      ims.resources r ON t.resource_id = r.resource_id 
       INNER JOIN 
      ims.resource_models rm ON r.resource_model_id = rm.resource_model_id 
       INNER JOIN 
      ims.resource_brands rb ON rm.resource_brand_id = rb.resource_brand_id 
       INNER JOIN 
      ims.resource_types rt ON rm.resource_type_id = rt.resource_type_id 
       INNER JOIN 
      ims.users u ON t.uid = u.uid 
       INNER JOIN 
      ims.transaction_types tt ON t.transaction_type_id = tt.transaction_type_id 
     WHERE 
      t.transaction_type_id = 3 
       AND YEAR(t.initiation_timestamp) = YEAR(CURDATE()) 
     UNION SELECT 
      CONCAT(u.name, ' ', u.surname) AS salesPerson, 
      o.closing_timestamp AS saleDate, 
      o.customer_id AS customerAccount, 
      tt.transaction_type AS transactionType, 
      r.resource_unique_value AS soldResource, 
      CONCAT(rb.resource_brand, 
        ' ', 
        rm.resource_model, 
        ' ', 
        rt.resource_type) AS resourceModel, 
      r.voucher_value_id as value 
     FROM 
      orders o 
       INNER JOIN 
      ims.users u ON o.initiation_uid = u.uid 
       INNER JOIN 
      ims.transaction_types tt ON o.order_type_id = tt.transaction_type_id 
       INNER JOIN 
      ims.resources r ON o.resource_id = r.resource_id 
       INNER JOIN 
      ims.resource_models rm ON r.resource_model_id = rm.resource_model_id 
       INNER JOIN 
      ims.resource_brands rb ON rm.resource_brand_id = rb.resource_brand_id 
       INNER JOIN 
      ims.resource_types rt ON rm.resource_type_id = rt.resource_type_id 
     WHERE 
      o.order_type_id = 4 
       AND order_status_id = 1 
       AND YEAR(o.closing_timestamp) = YEAR(CURDATE())"; 
$results = $conn->query($sql); 

$response = implode(",", array('salesPerson','saleDate','customerAccount','transactionType','soldResource','resourceModel','value')); 
$response .= "\n"; 

foreach($results as $row) 
{ 
    $response .= implode(",",array($row['salesPerson'], $row['saleDate'], $row['customerAccount'], $row['transactionType'], $row['soldResource'], $row['resourceModel'], $row[escape('value')])); 
    $response .= "\n"; 
} 

header('Content-type: text/csv'); 
header('Content-disposition: attachment;filename=salesExport.csv'); 

echo $response; 

return; 

}

次のスクリーンショットは、私が何を意味するかを説明します:Click Here

そして、ここでは、私はメソッドを呼び出しています方法です:

<?php 
require_once 'core/init.php'; 
include_once ("header.php"); 

if ($user->isLoggedIn()){ 

//check if user has permission 
if ($user->hasPermission('reports') || $user->hasPermission('allAccess')){ 

    $inventory = new inventory(); 

    if(isset($_POST['exportSalesCsv'])){ 

     $inventory->exportSalesCsv(); 

    }... 
+0

そして:

header('Location: ' . $url_to_file); return; 

別の解決策は、ブラウザがcsvファイルとしてそれを解釈するように、ファイルに印刷が、変数に書き込み、CSVファイルを返すために、ヘッダーとそれを返すことではありません何が起こるはずですか? –

+0

@u_mulderポップアップを促すメッセージが表示され、ユーザにダウンロード場所を問い合わせてください – Iommiund

+0

次に、適切なヘッダーを設定します。 –

答えて

1

スクリプトで以下のソースコードがありますファイルをディスクに保存するだけで、データが入力されますが、コールの応答では返されません。

作成したファイルにApacheの/ nginxサーバーからアクセスできれば、そのファイルにリダイレクトできます。これを関数の最後に追加します。

$results = $conn->query($sql); 

$response = implode(",", array('salesPerson','saleDate','customerAccount','transactionType','soldResource')); 
$response .= "\n"; 

foreach($results as $row) 
{ 
    $response .= implode(",",array($row['salesPerson'], $row['saleDate'], $row['customerAccount'], $row['transactionType'], $row['soldResource'])); 
    $response .= "\n"; 
} 

header('Content-type: text/csv'); 
header('Content-disposition: attachment;filename=' . strtotime("now") . '.csv'); 

echo $response; 

return; 
+0

文字列の '+ ='はjavascriptのアプローチです。 –

+0

PHPでも動作します。 –

+0

しかし、あなたはあなたが期待していないだろうhttps://3v4l.org/T5WgK –

関連する問題