Chris99391
Chris99391

Reputation: 581

Add a header to each column

I would like to add to my script a header for each of my columns, here is my code:

 $products = $order->getProducts();

    // Création CSV détail de commande
    $filename = $order->reference . '_details.csv';
    if (!$fp = fopen($this->exportDir . '/' . $filename, "w")) {
        throw new Exception('Impossible de créer fichier export : ' . $this->exportDir . '/' . $filename);
    }

    $header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];

    fputcsv($fp, $header);

    foreach ($products as $product) {
        $data = array();
        $data[] = $order->reference;
        $data[] = (string)$product['product_reference'];
        $data[] = $product['product_quantity'];
        $data[] = number_format($product['unit_price_tax_incl'], 2);
        $data[] = number_format($product['total_price_tax_incl'], 2);

        fputcsv($fp, $data, $this->csvDelimiter, $this->csvEnclosure);
    }

    fclose($fp);

header

I try different thing but the header puts that on a cell .. thank you for your help.

Upvotes: 0

Views: 51

Answers (1)

MonkeyZeus
MonkeyZeus

Reputation: 20737

You need to specify the header delimiter and stuff so that it matches your data:

Change:

$header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];

fputcsv($fp, $header);

into

$header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];

fputcsv($fp, $header, $this->csvDelimiter, $this->csvEnclosure);

Upvotes: 1

Related Questions