Output as csv

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Berrow

    #1

    Output as csv

    I'm trying to output the results of a database query as a csv file

    The DB output is in $csv

    foreach($csv as $array){
    $line[]=implode($delim ited_by,$array) ."\n";
    }
    header("Content-Type: text/plain");
    header("Content-Disposition: inline");

    foreach($line as $output){
    print $output;


    On clicking the link to this file the results are output to the screen.
    I can right click and save the output but I'm prompted to save it as a
    ..php file. How can I make it output as the right kind ( .csv) of file?

    --
    Regards,

    Geoff Berrow
  • Bent Stigsen

    #2
    Re: Output as csv

    Geoff Berrow wrote:[color=blue]
    > I'm trying to output the results of a database query as a csv file
    >
    > The DB output is in $csv
    >
    > foreach($csv as $array){
    > $line[]=implode($delim ited_by,$array) ."\n";
    > }
    > header("Content-Type: text/plain");
    > header("Content-Disposition: inline");
    >
    > foreach($line as $output){
    > print $output;
    >
    >
    > On clicking the link to this file the results are output to the screen.
    > I can right click and save the output but I'm prompted to save it as a
    > .php file. How can I make it output as the right kind ( .csv) of file?[/color]

    There is a rfc stating "text/csv" as the mime-type, but it is quite
    recent, so probably not widely recognized.
    http://www.rfc-archive.org/getrfc.php?rfc=4180

    Presently, I think the common way is to set disposition to attachment
    and supply a filename, and let the OS figure out how to open it.
    header('Content-Disposition: attachment; filename="foo.c sv"');


    /Bent

    Comment

    • Geoff Berrow

      #3
      Re: Output as csv

      Message-ID: <44142622$0$470 27$edfadb0f@dre ad15.news.tele. dk> from Bent
      Stigsen contained the following:
      [color=blue]
      >There is a rfc stating "text/csv" as the mime-type, but it is quite
      >recent, so probably not widely recognized.
      >http://www.rfc-archive.org/getrfc.php?rfc=4180
      >
      >Presently, I think the common way is to set disposition to attachment
      >and supply a filename, and let the OS figure out how to open it.
      >header('Conten t-Disposition: attachment; filename="foo.c sv"');[/color]

      Thanks.

      I have found that this gives an option to open or dave the file,
      however, opening it does not work.

      But the following seems to work fine.

      header("Content-Type: text/csv");
      header("Content-Disposition: inline; filename=$_SESS ION[table].csv ");

      --
      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

      • Geoff Berrow

        #4
        Re: Output as csv

        Message-ID: <a7k81294tbp938 j1cj7oi6ng978q0 2476l@4ax.com> from Geoff
        Berrow contained the following:
        [color=blue]
        >header("Conten t-Type: text/csv");
        >header("Conten t-Disposition: inline; filename=$_SESS ION[table].csv ");[/color]

        OK, I'm exporting CSV files nicely now but I wondered how other people
        tackle the problem of the content containing quotes.

        I'm exporting the data like this

        "some data", "some more data", "yet more data"

        Eventually the file will be imported into Access.

        It occurred to me that if the data contained a double quote mark, then
        the file could be messed up.

        I've done a string replace and changed double quotes to single, but this
        doesn't feel like a very elegant or satisfactory solution so I wondered
        what other people do?

        --
        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

        • NC

          #5
          Re: Output as csv

          Geoff Berrow wrote:[color=blue]
          >
          > I'm exporting CSV files nicely now but I wondered how other
          > people tackle the problem of the content containing quotes.[/color]

          RFC 4180 specifies escaping a double quote by putting another double
          quote in front of it. However, RFC 4180's status is "Informational" ,
          and it notes the existence of "considerab le differences among
          implementations ". So you should consult the manual(s) for the software
          that you expect to read the CSV you are generating and see what they
          suggest.

          Cheers,
          NC

          Comment

          • Geoff Berrow

            #6
            Re: Output as csv

            Message-ID: <1142211766.819 712.82370@j33g2 000cwa.googlegr oups.com> from
            NC contained the following:
            [color=blue]
            >
            >RFC 4180 specifies escaping a double quote by putting another double
            >quote in front of it. However, RFC 4180's status is "Informational" ,
            >and it notes the existence of "considerab le differences among
            >implementation s". So you should consult the manual(s) for the software
            >that you expect to read the CSV you are generating and see what they
            >suggest.[/color]


            Thanks, I'll give it a try.
            --
            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

            • Toby Inkster

              #7
              Re: Output as csv

              NC wrote:
              [color=blue]
              > RFC 4180 specifies escaping a double quote by putting another double
              > quote in front of it. However, RFC 4180's status is "Informational" ,
              > and it notes the existence of "considerab le differences among
              > implementations ".[/color]

              The OP mentioned importing into Access. Access does indeed support this
              method of escaping.

              --
              Toby A Inkster BSc (Hons) ARCS
              Contact Me ~ http://tobyinkster.co.uk/contact

              Comment

              • adrian.price@gmail.com

                #8
                Re: Output as csv

                If you've got access to PHP 5.1+, you can just use the already
                RFC-compliant fputcsv() function:

                Format line as CSV and write to file pointer


                Comment

                • shriop

                  #9
                  Re: Output as csv

                  um, rfc where? there's no stinkin rfc...

                  adrian.price@gm ail.com wrote:[color=blue]
                  > If you've got access to PHP 5.1+, you can just use the already
                  > RFC-compliant fputcsv() function:
                  >
                  > http://us3.php.net/fputcsv[/color]

                  Comment

                  • shriop

                    #10
                    Re: Output as csv

                    ignore me. wasn't paying attention to the thread... interesting.

                    shriop wrote:[color=blue]
                    > um, rfc where? there's no stinkin rfc...
                    >
                    > adrian.price@gm ail.com wrote:[color=green]
                    > > If you've got access to PHP 5.1+, you can just use the already
                    > > RFC-compliant fputcsv() function:
                    > >
                    > > http://us3.php.net/fputcsv[/color][/color]

                    Comment

                    Working...