[eluser]Mhabub[/eluser]
Here is the code:
$file_path = 'csv_data/';
$csv_file_name = 'ogsi_'.date("Y-m-d_h-i-m").'.csv';
//chmod($file_path."/".$csv_file_name, 0777);
$fp = fopen($file_path.$csv_file_name, 'w');
$result_array = array();
$result_array[0] = "Date,Granular Totes,Granular Bags,Pellet Totes,Pellet Bags,Total Tons,Manhours,Tons/Manhour";
$i = 1;
foreach ($csv_record->result() as $rec){
$result_array[$i]=$rec->creation_date.','.$rec->granular_totes.','.$rec->granular_gags.','.$rec->pellet_totes.','.$rec->pellet_bags.','.$rec->total_tons.','.$rec->manhours.','.$rec->tons_by_manhour;
$i++;
}
foreach($result_array as $line_data){
fputcsv($fp, split(',', $line_data));
}
fclose($fp);
$this->load->helper('download');
$doc_file_name = $csv_file_name;
$doc_file_loc = file_get_contents($this->baseurl.'csv_data/'.$csv_file_name); // Read the file's contents
force_download($doc_file_name,$doc_file_loc);