<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
public function index()
{
}
function _example_output($output = null)
{
$this->load->view('our_template.php',$output);
}
public function users()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('subscription');
$output = $crud->render();
$state = $crud->getState();
$state_info = $crud->getStateInfo();
if($state == 'export')
{
$this->export_to_csv($state_info);
}
else
{
$this->load->view('our_template',$output);
}
}
public function export_to_csv($state_info = null)
{
$data = $this->get_common_data();
$data->order_by = $this->order_by;
$data->types = $this->get_field_types();
$data->list = $this->get_list();
$data->list = $this->change_list($data->list , $data->types);
$data->list = $this->change_list_add_actions($data->list);
$data->total_results = $this->get_total_results();
$data->columns = $this->get_columns();
$data->primary_key = $this->get_primary_key();
@ob_end_clean();
$this->_export_to_csv($data);
}
protected function _export_to_csv($data)
{
$string_to_export = "";
foreach($data->columns as $column){
$string_to_export .= $column->display_as."\t";
}
$string_to_export .= "\n";
foreach($data->list as $num_row => $row){
foreach($data->columns as $column){
$string_to_export .= $this->_trim_export_string($row->{$column->field_name})."\t";
}
$string_to_export .= "\n";
}
// Convert to UTF-16LE and Prepend BOMa
$string_to_export = "\xFF\xFE" .mb_convert_encoding($string_to_export, 'UTF-16LE', 'UTF-8');
$filename = "export-".date("Y-m-d_H:i:s").".csv";
header('Content-type: application/vnd.ms-excel;charset=UTF-16LE');
header('Content-Disposition: attachment; filename='.$filename);
header("Cache-Control: no-cache");
echo $string_to_export;
die();
}
}