Click to See Complete Forum and Search --> : [RESOLVED] increase width of csv file using php


raj_2006
09-24-2006, 08:20 AM
Hi all,

I am writing the DB data in a csv file using php.But the header column/cell needs to be increased in the csv file.

Is there any way to fix the width of each column of the header in csv file.

my code snippets looks like this

$file_pointer=fopen("transaction.csv", "w");
fwrite($file_pointer,"TRANSACTION ID,USERNAME,TRANSFER TO,DATE,AMOUNT,FEES,STATUS,NOTE\n");
..............
................
.................//while looping is going on
{
//fetching the fields
fwrite($file_pointer,"\n$TranId,$EmailId,$TransfferTo,$Date,$Amount,$Fees,$Status,$Note\n");
}


Please suggest if there is any way.

Thanks for your co-operation in advance.

Raj

ronverdonk
09-24-2006, 08:59 AM
You can the str_pad function to append your text with blanks to the specified number. As in:
fwrite($file_pointer,
str_pad("TRANSACTION ID", 20).",".
str_pad("USERNAME", 20) .",".
str_pad("TRANSFER TO",20) .",".
str_pad("DATE",20) .",".
str_pad("AMOUNT",20) .",".
str_pad("FEES",20) .",".
str_pad("STATUS",20) .",".
str_pad("NOTE",20).
"\n");

Ronald :cool:

NogDog
09-24-2006, 11:48 AM
Ummmm...CSV files are not fixed-width files. Why do you care how wide the headers are?

raj_2006
09-24-2006, 03:50 PM
I am caring about the headers as it will look nice if it shows the entire cell.....otherwise I have to increase the width of the cell manually.

I tried str_pad() but sorry its not working.


Its not a major issue but just thinking about how to do it.......

If there is any way then please let me know.

Thanks
Raj

NogDog
09-24-2006, 04:51 PM
Only thing I can think of (other than making the headers longer) is to add a printable character such as "." to the str_pad() calls, above:

str_pad("TRANSACTION ID", 20, '.')

However, I'm not sure even doing that will affect the default column widths of Excel when you first open a CSV file, but it won't hurt to try, I guess.