2017-04-01 2 views
0

Ich Abrufen von Daten aus der Datenbank und Herunterladen der Daten in eine CSV-Datei. die funktionalität funktioniert wie erwartet, aber die csv-datei, die heruntergeladen wird, enthält auch den seitenquellcode, ich weiß nicht, warum das passiert und habe schon lange versucht. Ich hoffe, dass mir jemand dabei helfen kann. unten ist der Quellcode:kann nicht nach CSV mit PDO exportieren

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; 

}

Der folgende Screenshot erklärt, was ich meine: Click Here

Und hier ist, wie ich die Methode am Aufruf:

<?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

Und was soll passieren? –

+0

@u_mulder Ich benötige eine Aufforderung zum Popup und frage den Benutzer nach dem Download-Speicherort – Iommiund

+0

Setzen Sie dann richtige Header. –

Antwort

1

Ihr Skript speichert nur die Datei auf der Festplatte und ist mit Daten gefüllt, aber es ist keine Antwort in der Antwort

zurückgegeben

Eine Lösung, wenn der Apache/Nginx-Server auf die erstellte Datei zugreifen kann, können Sie dorthin umleiten. Fügen Sie dies am Ende Ihrer Funktion hinzu.

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

Eine andere Lösung ist nicht auf Datei zu drucken, sondern für die Rückgabe einer CSV-Datei, auf eine Variable und zusammen mit den Header schreiben, so dass der Browser sie als csv-Datei interpretiert:

$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

'+ =' für Strings ist Javascript-Ansatz. –

+0

Funktioniert auch in PHP. –

+0

Aber Sie werden nicht bekommen, was Sie erwarten https://3v4l.org/T5WgK –

Verwandte Themen