0

I used the CSV library to export data from the database. The problem is that the Arabic charters appear as ط¯ظˆظƒظˆ ط§ط³ظˆط¯ .

I must change the Unicode to utf-8.

I tried the following line but it did not work.

header('Content-Type: text/csv; charset=utf-8');

The function is

function exportCSV(){
$data=$this->m->excel($allocation,$category);

            $this->load->helper('csv');
            header('Content-Type: text/csv; charset=utf-8');
            query_to_csv($data, TRUE, 'Balance.csv');
}

Thanks

Syam
  • 169
  • 1
  • 10

1 Answers1

0

I found another lines exports the result out of query_to_csv and the problem solved.

function exportCSV(){
//$this->load->helper('csv');
            $balance=$this->m->excel($allocation,$category);
            //query_to_csv($balance, TRUE, 'Balance.csv');
            //////
            //Step 3: Execute the query to export data from MySQL table
            $data = $balance->result_array();  //Fetch result in array format.

//Step 4: Write data into CSV file and save it on server.
            $fileName = 'Balance-'.date('Y-m-d').'.csv'; //Set file name with current date.

            header("Content-Description: File Transfer");   //Set header for download file.

            header("Content-Disposition: attachment; filename=$fileName");

            header("Content-Type: application/csv; "); //Set content type for download file.

            $file = fopen('php://output', 'w'); //Open file pointer in write mode.

            fputs($file, "\xEF\xBB\xBF"); //Encode UTF-8 BOM in file pointer.
            fputs($file, "Allocation,Category,Item Name,SN,Warehouse,QTY,AVG,Amount,\r\n"); //Encode UTF-8 BOM in file pointer.

            foreach ($data as $row) {   //Write each row of data into CSV file.

                fputcsv($file, $row);

            }

            fclose($file); //Close file pointer after writing all data into CSV file.

            

}
Syam
  • 169
  • 1
  • 10