csv column width

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sailu_mvn
Forum Newbie
Posts: 1
Joined: Thu Nov 06, 2008 1:49 am

csv column width

Post by sailu_mvn »

I am generating a csv using php code below.

$cr = "\n";

$data = "Jan" . ',' . "Jancap" . ',' . "Feb" . ',' . "Febcap" . ','."Mar".','."Marcap".','."April".','."Aprilcap".','."May".','."MayCap".','."June".',
'."Junecap".','."July".','."Julycap".','."August".','."Augustcap".','."September".','."Septembercap".','."October".','."Octobercap".','."November".','."Novembercap".','."December".','."Decembercap".$cr;

$data .= jan.jpg . ',' .$image_name_january . ',' . feb.jpg . ',' . $image_name_january .march.jpg . ',' .$image_name_january . ',' . april.jpg . ',' . $image_name_january . may.jpg . ',' .$image_name_january . ',' . june.jpg . ',' . $image_name_january .july.jpg . ',' .$image_name_january . ',' . august . ',' . $image_name_january .','.september.jpg . ',' .$image_name_january . ',' . october.jpg . ',' . $image_name_january .','.november.jpg . ',' .$image_name_january . ',' . december.jpg . ',' . $image_name_january .$cr;

$csv_contents .= $data;

function write_to_file($filename, $stringtowrite, $writetype)
{
$filesession = fopen($filename,$writetype);
fwrite($filesession,"$stringtowrite");
fclose($filesession);
}
write_to_file('exportfile.csv',$csv_contents, 'w');


Everything is working fine..but when it comes to columns in csv, from June on, they are printed in the next line. I want them as headings.How can I sort this problem?

Pls advice.

Sailaja
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: csv column width

Post by onion2k »

CSV files have no formatting capability. They're just displayed however the application you're viewing it in wants. If you need to format the data in a specific way you'll need to convert it to a different format (eg HTML, PDF, Excel, etc).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: csv column width

Post by pickle »

They're on a new line because you've got a newline character in your first $data assignment, right after the comma after June.

I'd suggest simplifying your code:

Code: Select all

$data = 'January, Jancap, Feb, Febcap...etc';
There's no need to be concatenating like that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply