How to create nice looking CSV with codeigniter - codeigniter

I'm using codeigniter to develop a system.
My issue here is kinda easy for you I believe.
This is my codeignter code.
$delimiter = ",";
$newline = "\r\n";
$data= $this->dbutil->csv_from_result($MYSQLRESULT,$delimiter, $newline);
write_file('./public/uploads/client.csv', $data);
But when it creates the csv file, it looks like this
Is there a way to make it look nice? I want headers stay seperated and all fields as well. I dont want commas or anything.
Help please. I think problem is about my delimiters but I don't know what to do.
EDIT: I want it to look like this:

tell excel the separator character with sep=, on the first line
write_file('./public/uploads/client.csv', "sep=,\n" . $data);

Related

How to preserve new line \n with laravel sanitizer

I'm trying to store text in my database with \n, so that there can be new lines whenever the admin wants. The problem I am getting is that automatically it seems laravel is sanitizing all strings that get saved to the DB. So it's saving it as \n. And then when I retrieve it (i retrieve and display with vue components not a blade file), it doesn't format it as a new line. how and what am i supposed to do. what is the right way of achieving being able to save a paragraph with new lines made with \n?
I think you need to convert the \n to be. you can do something like this:
<?php
echo nl2br("foo isn't\n bar");
?>

Laravel 5.2 Maatwebsite Excel::load not parsing Japanese characters

I'm using Maatwebsite excel v ~2.1.0 for my Laravel 5.2 project. My problem is that the japanese characters are not rendered as seen the photo below. As you can see, I have 23 heading rows and the displayed data only are only English characters. Columns that uses Japanese characters are null.
This is my data in my CSV file.
This is my approach in loading the CSV file:
Excel::load(request()->file('file'), function($reader) {
$results = $reader->all();
dd($results);
});
What should I change to make the Japanese characters readable?
Okay after exploring more, I found out the answer. In excel.php file, I changed 'to_ascii' => true, to 'to_ascii' => false and my problem is fixed. Hope this helps someone in the near future.

How can I write an array to a file with quotes around every value?

I'm trying to write an array to a CSV file. In the past, I've just used:
my_array.to_csv
and quotes be damned. But I have to conform on this particular file I'm writing to the CSV "standard". That means that, where this:
a,b,c,"d, with a comma",e
was satisfactory before, now I must output:
"a","b","d, with a comma","e"
There must be some easy way, but I can't find it.
I tried:
x.map{|v| '"' + v + '"'}.to_csv
but the file ended up with:
"""a""","""b""","""c"""
I've tried a bunch of variations on that. I ALWAYS end up with 3 quote characters.
The only thing that works is this:
fout.puts x.map{|v| "\"#{v}\"" }.to_csv.gsub('"""','"')
Which of course is hideous. Any help is appreciated!
Simply add an option:
my_array.to_csv(force_quotes:true)
You can check out more options here
better way follow
assuming a is array initially
a ="'" + a.join("','") + "'"

C# MVC3 and non-latin characters

I have my database results (áéíóúàâêô...) and when I display any of this characters I get codes like:
á
My controller is like this:
ViewBag.EstadosDeAlma = (from e in db.EstadosDeAlma select e.Title).ToList();
My cshtml page is like this:
var data = '#foreach (dynamic item in ViewBag.EstadosDeAlma){ #(item + " ") }';
In addition, if I use any rich text editor as Tiny MCE all non-latin characters are like this too.
What should I do to avoid this problem?
What output encoding are you using on your web pages? I would suggest using UTF-8 since you want a lot of non-ascii characters to work.
I think you should HTML encode/decode the values before comparing them.
Since you are using jQuery you can take advantage of the encoding functions built-in into it. For example:
$('<div/>').html('& #225;gil').html()
gives you "ágil" (notice that I added an extra space between the & and the # so that stackoverflow does not encode it, you won't need it)
This other question has more information about this.
HTML-encoding lost when attribute read from input field

Ruby - Writing Hpricot data to a file

I am currently doing some XML parsing and I've chosen to use Hpricot because of it's ease of use and syntax, however I am running into some problems. I need to write a piece of XML data that I have found out to another file. However, when I do this the format is not preserved. For example, if the content should look like this:
<dict>
<key>item1</key><value>12345</value>
<key>item2</key><value>67890</value>
<key>item3</key><value>23456</value>
</dict>
And assuming that there are many entries like this in the document. I am iterating through the 'dict' items by using
hpricot_element = Hpricot(xml_document_body)
f = File.new('some_new_file.xml')
(hpricot_element/:dict).each { |dict| f.write( dict.to_original_html ) }
After using the above code, I would expect that the output look like the following exactly like the XML shown above. However to my surprise, the output of the file looks more like this:
<dict>\n", " <key>item1</key><value>12345</value>\n", " <key>item2</key><value>67890</value>\n", " <key>item3</key><value>23456</value\n", " </dict>
I've tried splitting at the "\n" characters and writing to the file one line at a time, but that didn't seem to work either as it did not recognize the "\n" characters. Any help is greatly appreciated. It might be a very simple solution, but I am having troubling finding it. Thanks!
hpricot_element = Hpricot::XML(xml_document_body)
File.open('some_new_file.xml', 'w') {|f| f.write xml_document_body }
Don't use an an xml parser if you want the original xml to be written. It is unnecessary. You should still use one if you want to further process the data, though.
Also, for XML, you should be using Hpricot::XML instead of just Hpricot.
My solution was to just replace the literal '\n' characters with line breaks and remove the extra punctuation by simply adding two gsubs that looked like the following:
f.write( dict.to_original_html.gsub('\n', "\n").gsub('" ,"', '') )
I don't know why I didn't see this before. Like I said, it might be an easy answer that I wasn't seeing and that's exactly how it turned out. Thanks for all the answers!

Resources