CSV export thing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Jacobs

    CSV export thing

    Hi all

    I am looking at the fputcsv function for something that I need. In summary,
    this is what I want to do:

    1. Go through a table
    2. Get all the columns and put them into an array
    3. write the array to a CSV file
    4. Go to the next row
    5. Append the table row into the CSV file
    6. Repeat for each row in the table

    The php site shows this:

    <?php

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

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

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

    fclose($fp);
    ?>

    I'm assuming that I need the 'a+' mode for opening the file and not 'w'?

    I'm ashamed to say that I have no idea how to loop through the table and put
    the columns into the array.

    What I'm coming up with at the moment is this:

    <?php do {

    $list = array (
    '
    $row_Recordset1['firstname'],
    $row_Recordset1['lastname'],
    $row_Recordset1['email'],
    $row_Recordset1['telephone']'
    '
    );

    $fp = fopen('/path/to/the/file.csv', 'a+');

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

    fclose($fp);

    } while ($row_Recordset 1 = mysql_fetch_ass oc($Recordset1) ); ?>

    Probably very inelegant!

    Does it look right though?

    Andy

  • Geoff Berrow

    #2
    Re: CSV export thing

    I noticed that Message-ID: <BE291C08.439C% andy@redcatmedi a.net> from
    Andy Jacobs contained the following:
    [color=blue]
    >Probably very inelegant!
    >
    >Does it look right though?[/color]

    Looks a bit long winded unless I'm missing summat.

    How about something like:

    //if not appending to existing file
    $csv= "firstname,last name,email,tele phone\n";
    //otherwise
    $csv="";

    while ($row_Recordset 1 = mysql_fetch_ass oc($Recordset1) ){
    $csv.=implode(" ,", $row_Recordset1 )."\n";
    }

    Then just write $csv to a file.

    Untested, obviously.

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • DH

      #3
      Re: CSV export thing

      Andy Jacobs wrote:[color=blue]
      > Hi all
      >
      > I am looking at the fputcsv function for something that I need. In summary,
      > this is what I want to do:
      >
      > 1. Go through a table
      > 2. Get all the columns and put them into an array
      > 3. write the array to a CSV file
      > 4. Go to the next row
      > 5. Append the table row into the CSV file
      > 6. Repeat for each row in the table[/color]

      The following was adapted from



      $header = '';

      $data = '';

      $export = @mysql_query("S ELECT * FROM $tb") or die(mysql_error ());

      $column_count = @mysql_num_fiel ds($export) or die(mysql_error ());

      for($i = 0; $i < $column_count; $i++){
      $header .= @mysql_field_na me($export, $i)."\t";
      }

      while($row = @mysql_fetch_ro w($export)) {
      $line = '';
      foreach($row as $value) {
      if ((!isset($value )) OR ($value == '')) {
      $value = "\t";
      }else{
      $value = str_replace('"' , '""', $value);
      $value = '"'.$value.' "'. "\t";
      }
      $line .= $value;
      }
      $data .= trim($line)."\n ";
      }

      $data = str_replace("\r ", '', $data);

      if($data == ''){
      $data = "\n(0) Records Found!\n";
      }

      @header("Conten t-type: application/octet-stream");
      @header("Conten t-Disposition: attachment; filename=$tb.xl s");
      @header("Pragma : no-cache");
      @header("Expire s: 0");
      print "$header\n$data ";
      mysql_close();

      Comment

      Working...