Results 1 to 10 of 10

Thread: Export To CSV

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    *wince* CSV? Why?

    CSV is all very well and good until you have one of your seperator characters in your data. Then you have to implement all sorts of things to get it working. You'd be better off using, say, XML. PHP has a standard config file format, too, which it can read in natively; see parse_ini_file().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    What you should do is use mysql_fetch_array to select the rows you want, and then format it so that it looks like a CSV format. From there, cut and paste it into Notepad and save it as .csv format.

    Example:
    Code:
    $result = mysql_fetch_array( $result2 );
    echo "$result2[row1] , $result2[row2]"
    Would look like this:

    info1 , info2

    Good luck

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Actually you need braces.
    Code:
    echo "{$result2['row1']} , {$result2['row2']}";
    If you're doing that, though, you might as well:
    Code:
    function mysql_fetch_all($rs) {
      $arr = array();
      while($row = mysql_fetch_array($rs))
        array_push($arr, $row);
      return $arr;
    }
    
    function mysql_to_csv($rs) {
      return implode("\n", array_map(create_function('$a', 'return implode(\',\', $a);'), mysql_fetch_all($rs)));
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks guys... Well I got it to run, but it doesn't save anything... This is what I have... Should I be using fputcsv instead? Oh and the reason it's a csv is so it can be imported into a simple gateway app on the web and they only take .csv unfortunately...

    PHP Code:
    $query "";
         
        
    $result mysql_query($query); 
        
    $num mysql_num_rows($result); 
      
      function 
    mysql_fetch_all($result) {
      
    $arr = array();
      while(
    $row mysql_fetch_array($result))
        
    array_push($arr$row);
      return 
    $arr;
    }

    function 
    mysql_to_csv($result) {
      return 
    implode("\n"array_map(create_function('$a''return implode(\',\', $a);'), mysql_fetch_all($result)));
    }
        
    include 
    'library/closedb.php';
    ?> 

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    it doesn't save anything...
    Why do you expect it to? Surprisingly enough, the mysql_to_csv() function converts a MySQL result resource to CSV. You haven't actually done anything with that data -- in fact, you haven't even called the function.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But when the page runs isn't it called? I don't understand...

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No... since I don't know what you want to do with the data, I provided two functions ("a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code"), the former of which is used by the latter to perform the conversion you required.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Um sorry that totally confuses me... 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?

  10. #10
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hiya guys ok worked it all out... This appears to be working correctly, does it look ok?

    PHP Code:
    $result mysql_query("");

           while(
    $row mysql_fetch_array($result)) { 
        
        
    $textString $row['MobileNumber'];
        
    $trimmedMobile preg_replace('/[^\d+]/'''$MobileNumber);

        
    $csv_output .= "$row[FirstName],$row[LastName],$trimmedMobile,$row[Email]\n";
        
        } 

       
    header("Content-type: application/vnd.ms-excel");
       
    header("Content-disposition: attachment; filename=" .$date.".xls");
       print 
    $csv_output;
       exit;  

    ?> 
    Last edited by tomyknoker; 08-21-2007 at 03:15 PM.

Bookmarks

Posting Permissions

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