php custom headers/column heads in csv export












1















I cannot get custom column heads to show in my csv file. When I use the code below, the column headers appear above every row of data:



header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$q1b.csv");

$output = fopen("php://output", "w");
fputcsv($output, array("Date","First","Last","ID"));

foreach($data as $rowList) {
fputcsv($output, $rowList);
}

fclose($output);


The csv file looks like below, with the data above the column heads:




"10/11/2018" "Test" "Person" "id info"

"Date" "First" "Last" "ID"




EDIT:::



The following code is $data:



$data =  """.$date_formatted."","".$FIRST_NAME."","".$LAST_NAME."","".$Identify_Prospect_UPD.""n";
echo $data;


It looks like I have the csv tied to the $data echo, which I don't think I want. Weird!










share|improve this question




















  • 1





    Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

    – git-e-up
    Nov 20 '18 at 21:48













  • I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

    – Darragh Enright
    Nov 20 '18 at 22:19











  • My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

    – user1469
    Nov 26 '18 at 14:34
















1















I cannot get custom column heads to show in my csv file. When I use the code below, the column headers appear above every row of data:



header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$q1b.csv");

$output = fopen("php://output", "w");
fputcsv($output, array("Date","First","Last","ID"));

foreach($data as $rowList) {
fputcsv($output, $rowList);
}

fclose($output);


The csv file looks like below, with the data above the column heads:




"10/11/2018" "Test" "Person" "id info"

"Date" "First" "Last" "ID"




EDIT:::



The following code is $data:



$data =  """.$date_formatted."","".$FIRST_NAME."","".$LAST_NAME."","".$Identify_Prospect_UPD.""n";
echo $data;


It looks like I have the csv tied to the $data echo, which I don't think I want. Weird!










share|improve this question




















  • 1





    Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

    – git-e-up
    Nov 20 '18 at 21:48













  • I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

    – Darragh Enright
    Nov 20 '18 at 22:19











  • My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

    – user1469
    Nov 26 '18 at 14:34














1












1








1


0






I cannot get custom column heads to show in my csv file. When I use the code below, the column headers appear above every row of data:



header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$q1b.csv");

$output = fopen("php://output", "w");
fputcsv($output, array("Date","First","Last","ID"));

foreach($data as $rowList) {
fputcsv($output, $rowList);
}

fclose($output);


The csv file looks like below, with the data above the column heads:




"10/11/2018" "Test" "Person" "id info"

"Date" "First" "Last" "ID"




EDIT:::



The following code is $data:



$data =  """.$date_formatted."","".$FIRST_NAME."","".$LAST_NAME."","".$Identify_Prospect_UPD.""n";
echo $data;


It looks like I have the csv tied to the $data echo, which I don't think I want. Weird!










share|improve this question
















I cannot get custom column heads to show in my csv file. When I use the code below, the column headers appear above every row of data:



header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$q1b.csv");

$output = fopen("php://output", "w");
fputcsv($output, array("Date","First","Last","ID"));

foreach($data as $rowList) {
fputcsv($output, $rowList);
}

fclose($output);


The csv file looks like below, with the data above the column heads:




"10/11/2018" "Test" "Person" "id info"

"Date" "First" "Last" "ID"




EDIT:::



The following code is $data:



$data =  """.$date_formatted."","".$FIRST_NAME."","".$LAST_NAME."","".$Identify_Prospect_UPD.""n";
echo $data;


It looks like I have the csv tied to the $data echo, which I don't think I want. Weird!







php export-to-csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 14:35







user1469

















asked Nov 20 '18 at 21:06









user1469user1469

113




113








  • 1





    Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

    – git-e-up
    Nov 20 '18 at 21:48













  • I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

    – Darragh Enright
    Nov 20 '18 at 22:19











  • My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

    – user1469
    Nov 26 '18 at 14:34














  • 1





    Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

    – git-e-up
    Nov 20 '18 at 21:48













  • I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

    – Darragh Enright
    Nov 20 '18 at 22:19











  • My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

    – user1469
    Nov 26 '18 at 14:34








1




1





Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

– git-e-up
Nov 20 '18 at 21:48







Not sure I know enough to help, but what is $data? I think it would need to be a multidimensional array in this case.

– git-e-up
Nov 20 '18 at 21:48















I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

– Darragh Enright
Nov 20 '18 at 22:19





I edited your question to remove the first code block because it appeared to be duplicate, except for one difference which was a syntax error. I defined some $data locally and ran this code and I couldn't replicate the issue. Can you confirm that the code above matches the code you are running?

– Darragh Enright
Nov 20 '18 at 22:19













My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

– user1469
Nov 26 '18 at 14:34





My fault for the delay! That is the code I am running and for some reason the column headers appear below each row of data. I'll keep looking. There has to be a mistake in my php

– user1469
Nov 26 '18 at 14:34












1 Answer
1






active

oldest

votes


















0














Turns out it was "operator error" ... translation...me!



I didn't realize I had my header code inside a foreach loop!



Rats!!!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401533%2fphp-custom-headers-column-heads-in-csv-export%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Turns out it was "operator error" ... translation...me!



    I didn't realize I had my header code inside a foreach loop!



    Rats!!!






    share|improve this answer




























      0














      Turns out it was "operator error" ... translation...me!



      I didn't realize I had my header code inside a foreach loop!



      Rats!!!






      share|improve this answer


























        0












        0








        0







        Turns out it was "operator error" ... translation...me!



        I didn't realize I had my header code inside a foreach loop!



        Rats!!!






        share|improve this answer













        Turns out it was "operator error" ... translation...me!



        I didn't realize I had my header code inside a foreach loop!



        Rats!!!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 17:13









        user1469user1469

        113




        113
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401533%2fphp-custom-headers-column-heads-in-csv-export%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings