Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Export To CSV

  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Export To CSV

    Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Have a look at fputcsv()

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks I have read through that page over and over it doesn't really help me though... If ANYONE has a clean way of doing the following would be great. I have read about 25 posts from Google searches every single one different every single one using different PHP elements...

    What I want to do is pull out from my table into a csv file 5 columns, FirstName, LastName, Email, Mobile... I then want to add to the top of the .csv seperate headings... So basically I want to achieve this...

    Fname, Sname, Email, Mobile
    Tom, Smith, tom@smith.com, 0410000000
    James, Smith, james@smith.com, 0410000000

    I have tried every which way and can't get a result... I'm not sure where to go from this point... How do I write to the actual .csv file? I mean what do I need to add to the above code to get a file?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You read that page over and over but never thought to try the example? It's about as "clean" as you're going to find. Here's the example:

    PHP Code:
    <?php

    $list 
    = array (
        
    'aaa,bbb,ccc,dddd',
        
    '123,456,789',
        
    '"aaa","bbb"'
    );

    $fp fopen('file.csv''w');

    foreach (
    $list as $line) {
        
    fputcsv($fpsplit(','$line));
    }

    fclose($fp);
    ?>
    Once you see that work, you'll be able to replace the array called $list with your own array built from your database query. I'm not sure how much more straightforward it can get for you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •