2

I'm doing function to export CSV file in PHP. but when i do an export, i need to add information about total in number_format().. but display is wrong.

Here is my code

header('Content-Type: text/csv; charset=utf-8');  
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header('Content-Disposition: attachment; filename=' . $fileName ); 
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');

$file = fopen("report.csv", "w");  

fwrite($file, "Generate Report \r\n"); 
fwrite($file, "Total Sales, ".number_format($total)."\r\n");
fwrite($file, " "."\r\n");

the result is this

enter image description here

and that is wrong.

How do i can display the correct format number like "5,485" or maybe "5,485.00" in 1 cell on CSV file?

please help

3
  • 1
    fwrite($file, "Total Sales, \"".number_format($total)."\"\r\n"); Commented Dec 1, 2021 at 6:05
  • The fwrite() writes to an open file. The function will stop at the end of the file (EOF) or when it reaches the specified length, whichever comes first. Commented Dec 1, 2021 at 6:25
  • thankyou @user10099 it works!! Commented Dec 1, 2021 at 6:57

1 Answer 1

1
fwrite(resource $handle, string $string, int $length = ?): 

fwrite($file, "Total Sales, \"".number_format($total)."\"\r\n");

int fwrite() writes the contents of string to the file stream pointed to by handle.

Return Values fwrite() returns the number of bytes written, or false on error.

Sign up to request clarification or add additional context in comments.

2 Comments

Please share more details such that others can learn from your answer. How does this help to use proper formatting within that file?
@ferdinand it's helpful?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.