I’m using Excel CSV helper for CodeIgniter in PyroCMS and it works fine except it appends the page’s html source to the end of the file.
Here is what should be the last line followed by the beginning of the pages html;
18,10629,"2010-03-06 15:25:14","2010-03-06 15:43:01",905,6.4,53.7,9410,Server01,"2010-03-06 15:43:46",25.5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Meta data -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
It goes on like that until the end of the page’s html.
Here’s my call to it in my controller;
if(isset($_POST['csv'])){
$unlmited_result_array = $this->db->get()->result_array();
$top_row[0] = array_keys($unlmited_result_array[0]);
$toto = array_merge($top_row, $unlmited_result_array);
array_to_csv($toto, 'toto.csv');
}
And I’m using the following form to call it;
<?php
echo form_open('cip')
. form_hidden('Trucknumber', $_POST['Trucknumber'])
. form_hidden('VechicleRegistration', $_POST['VechicleRegistration'])
. form_hidden('from-date', $_POST['from-date'])
. form_hidden('to-date', $_POST['to-date'])
. form_submit('csv', $this->lang->line('cip:CSV'))
. form_close();
?>
In the controller, after displaying array_to_csv(), add a return. It will prevent the view from being loaded.
Another option: use a controller method to display the form and another one to display the CSV.