I am exporting data to CSV and facing one problem.
$data['name'] = "Test";
$data['category'] = "category1, category2, caegory3";
When I export code to CSV, it exports records but make separate column for category1, separate column for category 2 and separate column for category3.
Current Output
-------------------------------------------
| Name | Category | | |
--------------------------------------------
| TEst | category1 | category2 | category3 |
--------------------------------------------
Expected Output
------------------------------------------------------------
| Name | Category | | |
-------------------------------------------- ---------------
| TEst | category1,category2,category3 | | |
------------------------------------------------------------
What should I do so comma-separated String fall in same column?
You should have category values between ""
so the comma is not interpreted. You can change code like this:
$data['name'] = 'Test';
$data['category'] = '"category1, category2, caegory3"';
You should really take a look at fputcsv
. It does exactly what you want, with different escaping characters and so on.
http://php.net/manual/en/function.fputcsv.php
If you wish to output it to the buffer instead of a file you can use $fh = fopen('php://output', 'w');
to write to the client.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.