[eluser]LiorBroshi[/eluser]
Heya, 3 small updates for this plugin:
1. Fixed the non-utf8/ascii problem, now you can define which encoding you need and output with it (in the example i used windows-1255 -> hebrew).
2. Added option to use `labels` for the fields. instead of just showing the sql field name, you can give it a nice label for better output and understanding what is the data we are looking at, instead of 'is_newsletter' you can write 'Wants Newsletter' etc'.
Just throw in an array that the keys represent the field_name and the value represent the label value.
3. Again, return instead of the `no data..` output.
Enjoy!
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Excel library for Code Igniter applications
* Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
*/
if ( ! function_exists('to_excel'))
{
function to_excel($query, $filename='exceloutput',$headers_labels=array())
{
$ci =& get_instance();
$ci->load->helper('download');
$char_encoding = 'windows-1255';
$headers = ''; // just creating the var for field headers to append to below
$data = ''; // just creating the var for field data to append to below
$fields = $query->field_data();
if ($query->num_rows() == 0) {
return 'empty';
} else {
foreach ($fields as $field) {
if (array_key_exists($field->name,$headers_labels)) {
# We have a label, use it.
$headers .= iconv(mb_detect_encoding($headers_labels[$field->name]),$char_encoding,$headers_labels[$field->name]) . "\t";
}
else {
# Put field name
$headers .= $field->name . "\t";
}
}
foreach ($query->result() as $row) {
$line = '';
foreach($row as $value) {
if (( ! isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
$value = iconv(mb_detect_encoding($value),$char_encoding,$value);
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
#doPre($data);
#die();
force_download($filename . ".xls", $headers . "\n" . $data);
}
}
}
/* End of file excel_helper.php */
/* Location: ./application/helpers/excel_helper.php */