www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP download CSV

  1. #1
    Join Date
    Jan 2010
    Posts
    127

    PHP download CSV

    I'm trying to download some data into a CSV file.

    I've had a look around but I seem to only get my .php file downloaded instead of the specific data I need. (So my .csv file gets the <doctype> down to the </html>)

    What I am trying to do, is call some data from a MySQL database with php, this works fine though it excludes entry number 1 thats also fine as this entry wont have correct data anyway.

    PHP Code:
    header("Content-type: text/x-csv");
       
    header("Content-type: text/csv");
       
    header("Content-type: application/csv");
       
    header("Content-Disposition: attachment; filename=member_info.csv");
     
       while (
    $checked mysql_fetch_array($sql_check)){
        
            
    $variable $checked[0] . ', ' $checked[1] . ', ' $checked[2] . ', ' $checked[3] . ', ' $checked[4] . ', ' $checked[5] . ', ' $checked[6] . ', ' $checked[7] . ', ' $checked[8] . ', ' $checked[9] . ', ' $checked[10] . ', ' $checked[11] . ', ' $checked[12] . ', ' $checked[13] . ', ' $checked[14] . ', ' $checked[15] . ', ' $checked[16] . ', ' $checked[17] . ', ' $checked[18] .' <br />';
            echo 
    $variable;
        }
        while (
    $checked2 mysql_fetch_array($sql_check2)){
        
            
    $variable2 $checked2[0] . ', ' $checked2[1] . ', ' $checked2[2] . ', ' $checked2[3] . ', ' $checked2[4] . ', ' $checked2[5] . ', ' $checked2[6] . ', ' $checked2[7] . ', ' $checked2[8] . ', ' $checked2[9] . ', ' $checked2[10] . ', ' $checked2[11] . ', ' $checked2[12] . ', ' $checked2[13] . ', ' $checked2[14] .' <br />';
            echo 
    $variable2;

        }

       exit; 
    That is the code I am using, but it doesn't seem to work - and the dummy code I worked from also gave the same kind of file rather than any information I actually had inputed to test - not even 'echo "test";' worked in the file.

    Perhaps I didn't find very good code to work from in the first place, but if anyone can help me I'd be very grateful.

    As a side note this is being done in the back end of wordpress relating to a plugin I have developed, so I would rather not find another plugin just to download this file

    Ara
    L.U.N.E for anyone and everyone who has an interest in literature

  2. #2
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    Did you try it via Excel ?Just to see if it comes out...

    PHP Code:
    header("Pragma: public");
    header("Cache-Control: max-age=0");
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=member_info.xls");
    header("Content-Transfer-Encoding: binary"); 
    then the output would be delimited by tabs - use \t instead of , and \n instead of <br \>
    Absence of a result is a result, unless defined otherwise.

  3. #3
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785

    a dyslexic moment...

    <br />
    Absence of a result is a result, unless defined otherwise.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    This might be simpler:
    PHP Code:
    // set your headers, then...
    if(($fh fopen('php://output''w')) != false) {
      while (
    $checked mysql_fetch_array($sql_check)){
        
    fputcsv($fh$checked);
      }
      while (
    $checked2 mysql_fetch_array($sql_check2)){ 
        
    fputcsv($fh$checked2);
      }
      
    fclose($fh);

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles