Create Downloadable CSV File from PHP Script

Posted by Aphex22 on Stack Overflow See other posts from Stack Overflow or by Aphex22
Published on 2014-06-04T14:47:18Z Indexed on 2014/06/04 15:25 UTC
Read the original article Hit count: 287

Filed under:
|
|

How would I create a formatted version of the following PHP script as a downloadable CSV file from the code below (1.0)

At the moment the fputcsv function is currently dumping the unparsed PHP/HTML code into a CSV file. This is incorrect. The downloaded CSV file should contain the columns and rows generated from the code at (1.0) as shown in the image link below.

I've tried using the following code at the top of the PHP file:

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=amazon.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

$mysql_hostname = "";
$mysql_user = "";
$mysql_password = "";
$mysql_database = "";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

$sql = "select * from product WHERE on_amazon = 'on' AND active = 'on'";
$result = mysql_query($sql) or die ( mysql_error() );

// loop over the rows, outputting them
while ($sql_result = mysql_fetch_assoc($sql)) fputcsv($output, $sql_result);

1.0

The start of the code outputs the column headings for the CSV file:

 // set headers
    echo "
    item_sku, external_product_id, external_product_id_type, item_name, brand_name,
    manufacturer, product_description, feed_product_type, update_delete,
    part_number, model, standard_price, list_price, currency, quantity,
    product_tax_code, product_site_launch_date, merchant_release_date, restock_date ...
<br>";

And then follows PHP script for the column values

// load all stock
while ($line = mysql_fetch_assoc($result) ) {

?>

<?php

    $size_suffix = array ("",'_chain','_con_b','_con_c');
    $arrayLength = count ($size_suffix);

    for($y=0;$y<$arrayLength;$y++) {

    //Possible size array to loop through when checking quantity
    $con_size = array (36,365,37,375,38,385,39,395,40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);
    $arrlength=count($con_size);

    for($x=0;$x<$arrlength;$x++) {

    // check if size is available
    if($line['quantity_c_size_'.$con_size[$x].$size_suffix[$y]] > 0 ) {

        ?>

        <!-- item sku -->
        <?=$line['product_id']?>,

        <!-- external product id -->
        <?=$line['code_size_'.$con_size[$x].'']?>,

        <?
        // external product id type
        $barcode = $line['code_size_'.$con_size[$x]];
        $trim_barcode = trim($barcode);
        $count = strlen($trim_barcode);

        if ($count == 12)  {
            echo "UPC";
        }
        if ($count == 13) {
            echo "EAN";
        }
        elseif ($count < 12)  {
        echo " ";
        }
        ?>,

        <!-- item name -->
        <?=$line['title']?>,

        <?
        // brand_name
        $brand = $line['jys_brand'];
        echo ucfirst($brand);
        ?>,

        <?
        // manufacturer
        $brand = $line['jys_brand'];
        echo ucfirst($brand);
        ?>,

        <!-- product description -->
        <?=preg_replace('/[^\da-z]/i', ' ', $line['amazon_desc']) ?>,

        <!-- feed product type -->
        Shoes,
        , , ,

        <!-- standard price -->
        <?=$line['price']?>,
        ,

        <!-- currency -->
        GBP,

        <!-- quantity -->
        <?=$line['quantity_size_'.$con_size[$x].$size_suffix[$y]]?>,
        ,

        <!-- product site launch date -->
        <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,

        <!-- merchat release date -->
        <?=$line['added_y']?>-<?=$line['added_m']?>-<?=$line['added_d']?>,
        , , , ,

        <!-- item package quantity -->
        1,
        , , , ,

        <!-- fulfillment latency -->
        2,

        <!-- max aggregate ship quantity -->
        1,

        , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

        <!-- main image url, url1, url2, url3 -->
        http://www.getashoe.co.uk/full/<?=$line['product_id']?>_1.jpg,
        http://www.getashoe.co.uk/full/<?=$line['product_id']?>_2.jpg,
        http://www.getashoe.co.uk/full/<?=$line['product_id']?>_3.jpg,
        http://www.getashoe.co.uk/full/<?=$line['product_id']?>_4.jpg,

        , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
        , , , , , , , , , , , , , ,

        <!-- heel height -->
        <?=$line['heel']?>,

        , , , , , , , , , ,

        <!-- colour name -->
        <?=$line['colour']?>,

        <!-- colour map -->
        <?

        $colour = preg_replace('/[()]/i', ' ', $line['colour']);

        if (preg_match( '/[\/].*/i', $colour))  {

            echo 'Multicolour';
        }
        if (preg_match( '/off.*/i', $colour))
        {
            echo 'Off-White';
        }
        elseif( preg_match( '/white.*/i', $colour))
        {
            echo 'White';
        }
        elseif( preg_match( '/moro.*/i', $colour))
        {
            echo 'Brown';
        }
        elseif( preg_match( '/morado.*/i', $colour))
        {
            echo 'Purple';
        }
        elseif( preg_match( '/cream.*/i', $colour))
        {
            echo 'Off-White';
        }
        elseif( preg_match( '/pewter.*/i', $colour))
        {
            echo 'Silver';
        }
        elseif( preg_match( '/yellow.*/i', $colour))
        {
            echo 'Yellow';
        }
        elseif( preg_match( '/camel.*/i', $colour))
        {
            echo 'Beige';
        }
        elseif( preg_match( '/navy.*/i', $colour))
        {
            echo 'Blue';
        }
        elseif( preg_match( '/tan.*/i', $colour))
        {
            echo 'Brown';
        }
        elseif( preg_match( '/rainbow.*/i', $colour))
        {
            echo 'Multicolour';
        }
        elseif( preg_match( '/orange.*/i', $colour))
        {
            echo 'Orange';
        }
        elseif( preg_match( '/leopard.*/i', $colour))
        {
            echo 'Multicolour';
        }
        elseif( preg_match( '/red.*/i', $colour))
        {
            echo 'Red';
        }
        elseif( preg_match( '/pink.*/i', $colour))
        {
            echo 'Pink';
        }
        elseif( preg_match( '/purple.*/i', $colour))
        {
            echo 'Purple';
        }
        elseif( preg_match( '/blue.*/i', $colour))
        {
            echo 'Blue';
        }
        elseif( preg_match( '/green.*/i', $colour))
        {
            echo 'Green';
        }
        elseif( preg_match( '/brown.*/i', $colour))
        {
            echo 'Brown';
        }
        elseif( preg_match( '/grey.*/i', $colour))
        {
            echo 'Grey';
        }
        elseif( preg_match( '/black.*/i', $colour))
        {
            echo 'Black';
        }
        elseif( preg_match( '/gold.*/i', $colour))
        {
            echo 'Gold';
        }
        elseif( preg_match( '/silver.*/i', $colour))
        {
            echo 'Silver';
        }
        elseif( preg_match( '/multi.*/i', $colour))
        {
            echo 'Multicolour';
        }
        elseif( preg_match( '/beige.*/i', $colour))
        {
            echo 'Beige';
        }
        elseif( preg_match( '/nude.*/i', $colour))
        {
            echo 'Beige';
        }

        ?>,  


        <!-- size name -->
        <? echo $con_size[$x];?>,

        <!-- size map -->
        <?

        if ($con_size[$x] == 36) {

            echo "3 UK";
        }
        elseif ($con_size[$x] == 37 ) {

            echo "4 UK";
        }
        elseif ($con_size[$x] == 38) {

            echo "5 UK";
        }
        elseif ($con_size[$x] == 39 ) {

            echo "6 UK";
        }
        elseif ($con_size[$x] == 40 ) {

            echo "7 UK";
        }
        elseif ($con_size[$x] == 41) {

            echo "8 UK";
        }
        elseif ($con_size[$x] == 42) {

            echo "9 UK";
        }
        elseif ($con_size[$x] == 43) {

            echo "10 UK";
        }
        elseif ($con_size[$x] == 44 ) {

            echo "11 UK";
        }
        elseif ($con_size[$x] == 45 ) {

            echo "12 UK";
        }
        elseif ($con_size[$x] == 46 ) {

            echo "13 UK";
        }
        elseif ($con_size[$x] == 47 ) {

            echo "14 UK";
        }
        elseif ($con_size[$x] == 48 ) {

            echo "15 UK";
        }
        elseif ($con_size[$x] == 365) {

            echo "3.5 UK";
        }
        elseif ($con_size[$x] == 375 ) {

            echo "4.5 UK";
        }
        elseif ($con_size[$x] == 385) {

            echo "5.5 UK";
        }
        elseif ($con_size[$x] == 395 ) {

            echo "6.5 UK";
        }
        elseif ($con_size[$x] == 405 ) {

            echo "7.5 UK";
        }
        elseif ($con_size[$x] == 415) {

            echo "8.5 UK";
        }
        elseif ($con_size[$x] == 425) {

            echo "9.5 UK";
        }
        elseif ($con_size[$x] == 435) {

            echo "10.5 UK";
        }
        elseif ($con_size[$x] == 445 ) {

            echo "11.5 UK";
        }
        elseif ($con_size[$x] == 455 ) {

            echo "12.5 UK";
        }
        elseif ($con_size[$x] == 465 ) {

            echo "13.5 UK";
        }
        elseif ($con_size[$x] == 475 ) {

            echo "14.5 UK";
        }
        elseif ($con_size[$x] == 485 ) {

            echo "15.5 UK";
        }

        ?>,

        <br>
        <?
        // finish checking if size is available
    } } }

        ?>

I've included an image of how the CSV file should appear.

https://i.imgur.com/ZU3IFer.png

Any help would be great.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql