Results 1 to 5 of 5

Thread: Problem with script thats supposed to export to CSV

  1. #1
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with script thats supposed to export to CSV

    Hi all,

    I am having a problem with my script where when i try to export data from my database to a csv file it appends the html of the page to the bottom of the CSV file. It gives me all the necessary values I need, but with annoying html of the page at the end. I am not sure how to get past this issue so some tips would be appreciated. The code is as follows.

    PHP Code:

    case "csv":
                
    $main .= "<h1>Export to CSV Format</h1>";
                
    $main .= '<div class="orange_container">';
                
                
    $sql "SELECT id,date,fullname,street,city,zip,phone,email,club FROM registration";
                
    $results $db->qarray($sql);
                
    //
                // send response headers to the browser
                // following headers instruct the browser to treat the data as a csv file called export.csv
                //
                
    header'Content-Type: text/csv' );
                
    header'Content-Disposition: attachment;filename=export.csv' );
                
    //
                // output header row (if atleast one row exists)
                //
                
                
    if($results != false)
                {
                    foreach(
    $results as $result)
                    {
                        
    $fields = array($result['id'],$result['date'],$result['fullname'],$result['street'],$result['city'],$result['zip'],$result['phone'],$result['email'],$result['club']);
                           
    echocsv($fields);
                           
                    }
                    
    $main .= "CSV File created!";
                }
                else
                {
                    
    $main .= '<div id="error">Failed to create CSV File!</div>';
                }
                
                
    $main .= '</div>';
                break; 

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I'd need to see more of the code, but I imagine you're echoing $main somewhere, after the switch statement.

    You've put the header as a CSV file, so any content will be included in it, even the HTML you write afterwards. I'd also need to see what "echocsv" does. But you want to separate the code that creates the CSV, and the code that outputs that the CSV file was created.

    Maybe try using a redirect after the CSV has been created.

  3. #3
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have attached the file as a zip for you to look at the code.

  4. #4
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorrry forgot the file last post

  5. #5
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone have any ideas ? This is driving me nuts!

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
  •