0

I have a php file. This file is a copy of a feed file inside Opencart and its like this:

  <?php
    public function index() {
    $ids = $this->db->query("select * from t_product where quantity>0");
    foreach ($ids->rows as $id) {
    //Inside here i create php variables, each one includes information like description, url etc and at the end i place it here:
    $myrow  = $id . ',' . $model . ',' . $name . ',' . $product_url;
    }

    $this->response->addHeader('Content-type: text/csv');
    $this->response->setOutput($myrow);
    }
    ?>

When i use the above code, then when i run the php file in browser, i get a php file downloaded that has all my data that i need. My question is how could i export this in csv format?

I tried code from: http://code.stephenmorley.org/php/creating-downloadable-csv-files/ with no success at all. Any ideas?

Also i somewhere read that i have to escape single quotes, double quotes and commas. How do i do this?

EDIT:

I also tried this:

<?php
    public function index() {
    $ids = $this->db->query("select * from t_product where quantity>0");
    $fp = fopen('php://output', 'w');
 foreach ($ids->rows as $id) {
        //Inside here i create php variables, each one includes information like description, url etc and at the end i place it here:
        $myrow  = $id . ',' . $model . ',' . $name . ',' . $product_url;
        fputcsv($fp, $myrow);
        }
fclose($fp);
 $this->response->addHeader('Content-type: text/csv');
         $this->response->setOutput($fp);
}
?>

with no success!

9
  • Just use PHP's built-in fputcsv() function Commented Mar 30, 2017 at 8:40
  • I tried and got Resource id #102 Commented Mar 30, 2017 at 8:41
  • Show us the code that you tried using fputcsv(). Commented Mar 30, 2017 at 8:41
  • Have you tried simply renaming the downloaded file to .csv? Commented Mar 30, 2017 at 8:42
  • Yes and because i have greek characters i get a mess and not in correct format Commented Mar 30, 2017 at 8:45

1 Answer 1

1

I think you are not telling your browser to download file, to resolve this problem I just told browser in which file we are writing.

// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="demo.csv"'); 
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');

// create a file pointer connected to the output stream
$file = fopen('php://output', 'w');

// send the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));

// Sample data. This can be fetched from mysql too
$data = array(
array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'));

// output each row of the data
foreach ($data as $row)
{
 fputcsv($file, $row);
}

exit();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.