Thursday 18 April 2019

Output mysql query result in csv

1. Exporting data from mysql and then providing for downloading it using Codeigniter. it will also rename table columns fields as you required. it will create a csv file then download using force_download function. get_report function will be in a model and download is a controller function.

2. $table variable parameter denotes the name of table from which you want to get data , $header is the header information as column in csv file , You can add another case in switch condition to fetch another table record and add assign $header value as of table column. By doing this you can limit the only fields that needs to show in csv file when it will download.

3. UPLOAD_PATH_DOCUMENT_VIEW , you can define it in constant.php file in codeigniter mvc framwork like define('UPLOAD_PATH_DOCUMENT_VIEW ',FCPATH.'');

public function get_report($table,$search_gstin_id,$month,$year,$user_id,$form_id){
$this->load->dbutil();
$this->load->helper('file');
$month_in = '('.month_in_clause($month).')';
switch ($table) {
    case 'b2b':
        $header = array('gstin_uin_of_recipient', 'month', 'year',
 'receiver_name', 'invoice_number', 'invoice_date', 'invoice_value',
 'place_of_supply', 'reverse_charge', 'applicable_per_of_tax_rate', 'invoice_type',
 'e_commerce_gstin', 'rate', 'taxable_value', 'cess_amount',
 'hsn_no', 'hsn_uqc', 'hsn_total_quantity', 'created');
        break;
    case 'b2cs':
        $header = array('month', 'year', 'type', 
'place_of_supply', 'applicable_per_of_tax_rate',
 'rate', 'taxable_value', 'cess_amount', 'e_commerce_gstin', 'created');
        break;
    default:
        break;
 }
 $myheaer = array();
 foreach ($header as $key => $value) {
    $myheaer[] = $value." AS '".str_replace('_', ' ', ucfirst($value))."'";
 }
 $table_columns = implode(",", $myheaer);
 $delimiter = ",";
 $newline = "\r\n";
 $enclosure = '"';
 $q = "Select " . $table_columns . "  from " . $table . " 
 where user_id=" . $user_id . " and form_id=" . $form_id . " 
 and search_gstin_id=" . $search_gstin_id . " and month in " . $month_in . "";
 $query = $this->db->query($q);
 if ($query->num_rows() > 0) {
    $csvContent = $this->dbutil->csv_from_result($query, $delimiter, $newline, $enclosure);
    if (!write_file(UPLOAD_PATH_DOCUMENT_VIEW . '/document_view.csv', $csvContent)) {
        return "UNABLE-TO-WRITE";
    } else {
        return "WRITED";
    }
  } else {
    return NULL;
  }
}

4. reports function is the model function which you can make and just simple revert back the array from mysql database.

public function download() {
 $this->load->helper('download');
 $search_gstin_id = (int) $this->uri->segment(3, 0);
 $month = $this->uri->segment(4, 0);
 $year = (int) $this->uri->segment(5, 0);
 $report_type = $this->uri->segment(6, 0);
 $form_id = (int) $this->uri->segment(7, 0);
 $this->load->model('Gstin');
 $result=$this->Gstin->reports($report_type,$search_gstin_id,$month,$year,$user_id,$form_id);
 if(!empty($result)){
  if($result =='UNABLE-TO-WRITE'){
   $_SESSION['markfront']=alertmessage($message_type='alert-success',$message='UnableWrite');
   $this->session->mark_as_flash('markfront');
   redirect(base_url().'');
   exit;
 }else{
 $filename = $report_type . '_' . date('Ymd') . '.csv';
 $inputFileName=UPLOAD_PATH_GSTR1_DOCUMENT_VIEW.'/upload_document_view.csv';
 force_download($filename, file_get_contents($inputFileName));
 exit;
    }
  }  
 }

5. Below function is a helper function which builds just replace dash with comma and make a in clause like '01','03'...etc

function month_in_clause($months) {
    $month = "'" . str_replace("-", "','", $months) . "'";
    return $month;
}