1

I have array of data whom I want to create CSV file and want to download it in my browser. I am creating it using Ajax call.

Here is my Ajax code:

<script type="text/javascript" language="javascript">
$(document).ready(function(){
    $("#exportBtn").click(function(){
        $.ajax({
        url: "<?php echo WEB_URL; ?>process/adminProcess.php?page=exportFile",
        async: true,
        type: "POST",
        data: $('#reportForm').serialize(),
        beforeSend: function(){
            $("#file").html("Generating Your File");
                    }
            })
        });
    });
</script>

Here is my page code on which this call goes:

if(isset($_GET['page']) && $_GET['page']=="exportFile"){

    $data = $objadminViewFunctions->exportFile();

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=data.csv");
    header("Cache-Control: no-cache, no-store, must-revalidate");
    header("Pragma: no-cache"); 
    header("Expires: 0"); 

    $outstream = fopen('php://output', 'w');

    fputcsv($output, array('Serial No.', 'Report Number', 'Pallet Id', 'Type', 'Consignee', 'Damage', 'Damage Description', 'Label', 'Vessel', 'Location', 'Suryeyor'));

    foreach ($data as $sss) {
        fputcsv($output, $sss);
    }
    fclose($outstream);

 }

Here is the data in $data which I want to enter in the CSV file:

Array
(
    [0] => stdClass Object
        (
            [de_ID] => 1
            [report_ID] => 1
            [pallet_ID] => 4788
            [type] => Apple
            [consignee] => Dandrea
            [damage] => Stow Damage
            [damage_desc] => TT Damage
            [label] => Valdovinos
            [variety] => Flame Seedless
            [category_code] => Empty and/or Missing Cartons
            [pieces] => 2
            [hold] => 2
            [deck] => C
            [dDate] => 2014-12-08
            [e_ID] => 1
            [report_number] => 123
            [vessel] => 321
            [location] => Phoenix 602
            [eDate] => 2014-12-01
            [suryeyor] => Mohsin
        )

    [1] => stdClass Object
        (
            [de_ID] => 2
            [report_ID] => 3
            [pallet_ID] => 8696
            [type] => Peach
            [consignee] => Del Monte
            [damage] => Breakout Damage
            [damage_desc] => TT Damage
            [label] => Agricom
            [variety] => Mango
            [category_code] => Damage to contents of cartons
            [pieces] => 4
            [hold] => 2
            [deck] => P
            [dDate] => 2014-12-08
            [e_ID] => 3
            [report_number] => 526
            [vessel] => 748
            [location] => Atlanta 404
            [eDate] => 2014-12-01
            [suryeyor] => Amir
        )

)

I have tried with array data as well but no luck yet:

Array
(
    [0] => Array
        (
            [0] => 1
            [de_ID] => 1
            [1] => 1
            [report_ID] => 1
            [2] => 4788
            [pallet_ID] => 4788
            [3] => Apple
            [type] => Apple
            [4] => Dandrea
            [consignee] => Dandrea
            [5] => Stow Damage
            [damage] => Stow Damage
            [6] => TT Damage
            [damage_desc] => TT Damage
            [7] => Valdovinos
            [label] => Valdovinos
            [8] => Flame Seedless
            [variety] => Flame Seedless
            [9] => Empty and/or Missing Cartons
            [category_code] => Empty and/or Missing Cartons
            [10] => 2
            [pieces] => 2
            [11] => 2
            [hold] => 2
            [12] => C
            [deck] => C
            [13] => 2014-12-08
            [dDate] => 2014-12-08
            [14] => 1
            [e_ID] => 1
            [15] => 123
            [report_number] => 123
            [16] => 321
            [vessel] => 321
            [17] => Phoenix 602
            [location] => Phoenix 602
            [18] => 2014-12-01
            [eDate] => 2014-12-01
            [19] => Mohsin
            [suryeyor] => Mohsin
        )

    [1] => Array
        (
            [0] => 2
            [de_ID] => 2
            [1] => 3
            [report_ID] => 3
            [2] => 8696
            [pallet_ID] => 8696
            [3] => Peach
            [type] => Peach
            [4] => Del Monte
            [consignee] => Del Monte
            [5] => Breakout Damage
            [damage] => Breakout Damage
            [6] => TT Damage
            [damage_desc] => TT Damage
            [7] => Agricom
            [label] => Agricom
            [8] => Mango
            [variety] => Mango
            [9] => Damage to contents of cartons
            [category_code] => Damage to contents of cartons
            [10] => 4
            [pieces] => 4
            [11] => 2
            [hold] => 2
            [12] => P
            [deck] => P
            [13] => 2014-12-08
            [dDate] => 2014-12-08
            [14] => 3
            [e_ID] => 3
            [15] => 526
            [report_number] => 526
            [16] => 748
            [vessel] => 748
            [17] => Atlanta 404
            [location] => Atlanta 404
            [18] => 2014-12-01
            [eDate] => 2014-12-01
            [19] => Amir
            [suryeyor] => Amir
        )

)

1 Answers1

0

Your PHP is wrong. The function fputcsv() doesn't know how to handle objects, it expects the second parameter to be an array.

Try to fix it like this:

fputcsv($output, (array)$sss);
axiac
  • 68,258
  • 9
  • 99
  • 134
  • How can than I can fix it? – programming Dec 11 '14 at 13:10
  • I cannot tell about Ajax but the PHP code displays the header row and then only "PHP Warning: fputcsv() expects parameter 1 to be resource, null given" lines. If your error reporting is suppressed then the warnings are not displayed but neither the data. – axiac Dec 11 '14 at 13:12
  • I have tried it with array as well but do not works. – programming Dec 11 '14 at 13:13
  • You have two parts here: the `PHP` part and the `Ajax` part and you need to check them independently. First, open the URL of the `PHP` (`http://.../process/adminProcess.php?page=exportFile`) in a browser and check if works as expected (i.e. the browser asks you to save the file, it saves the file with the correct name and termination and the saved file contains the CSV data you had in `$data`. After this part works flawless (and only after that), concentrate on the Ajax part. – axiac Dec 11 '14 at 13:26
  • I have checked my placing my array data and call the url it is generating my csv but with empty data. Means no data in it. as I have placed some array data in my $data. – programming Dec 11 '14 at 13:30