johanvena Posted April 7, 2020 Share Posted April 7, 2020 I am trying to write post variables to a csv file but it writes everything in one line separated by comma <?php $list= array($_POST['purchases']); $file = fopen("purchases.csv", "w"); foreach ($list as $line) { fputcsv($file, $line); } fclose($file); ?> Result in purchases.csv file Marilyn,Nancy,Johan,Carol,Juanic,Shirley But I want every string value on separated line Marilyn Nancy Johan Carol Juanic Shirley Quote Link to comment https://forums.phpfreaks.com/topic/310487-how-to-write-one-column-array-to-csv-file/ Share on other sites More sharing options...
Barand Posted April 7, 2020 Share Posted April 7, 2020 try foreach ($_POST['purchases'] as $line) { fputcsv($file, $line); } Quote Link to comment https://forums.phpfreaks.com/topic/310487-how-to-write-one-column-array-to-csv-file/#findComment-1576491 Share on other sites More sharing options...
johanvena Posted April 7, 2020 Author Share Posted April 7, 2020 Thanx for reply. Tried it but gives error Warning: fputcsv() expects parameter 2 to be array, string given in /home1/mymobi/public_html/mybank/update.php on line 7 My Code <?php $file = fopen("purchases.csv", "w"); foreach ($_POST['purchases'] as $line) { fputcsv($file, $line); } fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310487-how-to-write-one-column-array-to-csv-file/#findComment-1576499 Share on other sites More sharing options...
Barand Posted April 7, 2020 Share Posted April 7, 2020 foreach ($_POST['purchases'] as $line) { fputcsv($file, [$line]); } ? Quote Link to comment https://forums.phpfreaks.com/topic/310487-how-to-write-one-column-array-to-csv-file/#findComment-1576501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.