CodeIgniter Forums

Full Version: csv_from_result() and rares characters?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]chefnelone[/eluser]
Hello,

I'm using csv_from_result(); to generate a csv file:


Code:
$this->load->dbutil();
$queryForCsv = $this->db->query("SELECT * FROM membership_area");
$delimiter = ";";
$dataForCsv = $this->dbutil->csv_from_result($queryForCsv, $delimiter);

$pathCsv = '/clients.csv';
write_file($pathCsv, $dataForCsv);

problem is: if there are texts in the membership_area table with rare characters (ñ,°, á, é...) generate a csv file with mistakes.

Examples:
generates 4º
Años generates A√±os

How can I solve this?
Thanks

El Forum

[eluser]chefnelone[/eluser]
I've got the answer for the characters:

Code:
$this->load->dbutil();
$queryForCsv = $this->db->query("SELECT * FROM membership_area");
$delimiter = ";";
$dataForCsv = $this->dbutil->csv_from_result($queryForCsv, $delimiter);

//NEW LINE TO SHOW CHARACTERS PROPERLY
$dataForCsv = chr(255).chr(254).mb_convert_encoding( $dataForCsv, 'UTF-16LE', 'UTF-8');

$pathCsv = '/clients.csv';
write_file($pathCsv, $dataForCsv);

...but now I JUST get 1 column in Exell, when it should be 5 columns. Something like:
myname, myphone, myage, mycity, mycountry (all in one cell instead of 5 cells )

I know the problem is $delimiter = ";";
I've tried with:
* not setting a delimiter (2nd parameter of csv_from_result)
* and with : $delimiter = ",";

but isn't working...

El Forum

[eluser]Unknown[/eluser]
Hi! try this...

Code:
$this->load->dbutil();
$queryForCsv = $this->db->query("SELECT * FROM membership_area");
$delimiter = ";";
$dataForCsv = $this->dbutil->csv_from_result($queryForCsv, $delimiter);

//NEW LINE TO SHOW CHARACTERS PROPERLY
$dataForCsv = utf8_decode( $dataForCsv );

$pathCsv = '/clients.csv';
write_file($pathCsv, $dataForCsv);