I am using csv_helper.php file in helpers for exporting. It is grabing the results from mysql but showing the results only instead of downloading ! Here's the csv_helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('array_to_csv'))
{
function array_to_csv($array, $download = "")
{
if ($download != "")
{
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="' . $download . '"');
}
ob_start();
$f = fopen('php://output', 'w') or show_error("Can't open php://output");
$n = 0;
foreach ($array as $line)
{
$n++;
if ( ! fputcsv($f, $line))
{
show_error("Can't write line $n: $line");
}
}
fclose($f) or show_error("Can't close php://output");
$str = ob_get_contents();
ob_end_clean();
if ($download == "")
{
return $str;
}
else
{
echo $str;
}
}
}
if ( ! function_exists('query_to_csv'))
{
function query_to_csv($query, $headers = TRUE, $download = "")
{
if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
{
show_error('invalid query');
}
$array = array();
if ($headers)
{
$line = array();
foreach ($query->list_fields() as $name)
{
$line[] = $name;
}
$array[] = $line;
}
foreach ($query->result_array() as $row)
{
$line = array();
foreach ($row as $item)
{
$line[] = $item;
}
$array[] = $line;
}
echo array_to_csv($array, $download);
}
}
And here's the controller function:
public function exportUser() {
$this->load->database();
$query = $this->db->get('user');
$this->load->helper('csv');
query_to_csv($query, TRUE, 'toto.csv');
}
And in the view page it is showing the results: user_id,user_name,user_email,user_pass,user_phone,user_country,user_city,user_zip,user_address,user_type,user_status 53,abcdef,[email protected],12,1,,0,,,Student,1 54,aws,[email protected],12,12,Afghanistan,Kapisa,,,"Resource Person",0 55,onti,[email protected],12,12,,0,,,"Registered User",1 56,edf,[email protected],12,12,Albania,Bulqize,,dewde,"Admin User",1 58,meena,[email protected],,,,,,,"Registered User",0 61,nisat,[email protected],,,,,,,"Registered User",0
but not downloading ! Tried Chrome and mozilla both....
What to do???
Thank you in advance !
Content-Disposition: attachment, notattachement.