0

I am trying to create a csv in php with products from magento but my header doesn't show up in the csv file, just the products.

If i put the fputcsv for the headings inside the foreach it display the header then one product then the header again and another product and so on...

    ##### Print product data ####################################################

    $headings = ['category', 'manufacturer', 'productid', 'identifier', 'name', 'description', 'product_url', 'image_url', 'price','show_product', 'availability', 'delivery_cost'];
    $fh = fopen('php://output', 'w');

    ob_start();
    fputcsv($fh, $headings);

    foreach($ALL_PRODS as $productId) {

        // If we've sent this one, skip the rest - this is to ensure that we do not get duplicate products
        if (@$already_sent[$productId] == 1) continue;

        $PRODUCT = array();
        $PRODUCT = smfeed_get_product_details($productId);

        if ($PRODUCT['show_product'] == 1 ) {

        fputcsv($fh, $PRODUCT);

        $string = ob_get_clean();

        $filename = 'csv_' . date('Ymd') . '_' . date('His');

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"$filename.csv\";");
        header("Content-Transfer-Encoding: binary");

            $prod_count ++;

            // Debuging
            if (isset($_GET['debug'])) {
                $cnt_prod ++;

                if (@$_GET['stats'] == "off") {
                }
                else {
                    echo $cnt_prod . "."; 
                    echo "\t" . number_format(microtime(true) - $time, 3) . "s \n"; 
                    $time = microtime(true);
                    echo "\t" . number_format(memory_get_usage()/1048576, 3) . "Mb\n";
                }

            }

            // Limit displayed products
            if ($limit > 0 && $prod_count >= $limit && !isset($_GET['pg'])) {

                // Debuging
                if (isset($_GET['debug'])) {
                    echo "\n" . $cnt_prod . "products displayed \n";
                    echo "\npage loaded in " . number_format(microtime(true) - $time_start, 3) . "s \n"; 
                    echo number_format(memory_get_usage()/1048576, 3) . "Mb\n";
                }
                exit;
            }

        }
        $already_sent[$productId] = 1;
    }
    exit($string);

1 Answer 1

0

ob_start(); starts output buffering.

Buffer contains the header line.

After the first product output is done, output buffering is ended by ob_get_clean();

Since $string = ob_get_clean(); assignment is called in the loop, its content being rewritten by false value (since the buffering was ended before) therefore final exit($string); does not output anything.

There are many other issues in your code. For example, HTTP headers will be set over again and again for each item in the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.