Jump to content

csv column width


sailu_mvn

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/131598-csv-column-width/
Share on other sites

This should work

 

$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');

 

 

but you can do easier:

 

$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');

 

Link to comment
https://forums.phpfreaks.com/topic/131598-csv-column-width/#findComment-683788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.