How To write one column array to csv file

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

just take a look at the structure, keep in mind that CSV is two-dimensional

$list= array($_POST['purchases']);
var_dump($list);

$list= $_POST['purchases'];
var_dump($list);

Thanx for your reply Just did a var_dump for array($_POST[‘purchases’]);

Got This.

array(1) { [0]=> array(6) { [0]=> string(7) "Marilyn" [1]=> string(5) "Nancy" [2]=> string(5) "Johan" [3]=> string(5) "Carol" [4]=> string(6) "Juanic" [5]=> string(7) "Shirley" } }

How to fputcsv so they will not show all on one line but each item on its own line as I want to use in listbox

Then you won’t want to use fputcsv as it’s just a straight text file, not a csv.

What function can I use

Why does that mean that they have to be on a separate line in the file? Surely you could just read them in, explode() them into an array and create the list box? Or do you have some other code that cannot be changed that generates the list box?

I have this code to populate my listbox. Any suggestions

<div class="column">
     <td align=center valign=middle>
        
<h1>Purchases List</h1>
<form action="upload.php" method="post" enctype="multipart/form-data"><br />
    <input type=button class="master" name=b1 id=b1 value='Move >'>
    <input type=button class="master" name=b2 id=b2 value='< Remove'>
<select name=purchases[] id=purchases class=master>
<?php   
    $file = fopen("purchases.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $purchases = $row[0];
?>
    <option value="<?php echo $purchases;?>"><?php echo $purchases;?></option>
<?php
    }
?>
</select>
<input type="submit" value="Save File" name="master">
</td>

So the selected items will over write purchases.csv file

so don’t enclose the list into an array, but every item in an array before writing it to csv.

Ok Got half of it. Do not over complicate as you stated a csv is 2 dimension and what I am doing is a straight flat txt file so I can just write to a txt file

<?php

file_put_contents("purchases.txt", $_POST["purchases"]);

?>

Done it this way and the result is just one line output can you help so it will write one item per line

MarilynNancyJohanCarolJuanicShirley

Thanx for putting me in the right direction

Got IT!

<?php

foreach ($_POST["purchases"] as $key => $value) {
    $result.=$value. "\n";
}
file_put_contents('purchases.txt',$result);

?>

And the Output

Marilyn
Nancy
Johan
Carol
Juanic
Shirley

just use implode() then.

<?php   
$file = fopen("purchases.csv", "r");
$row = fgetcsv($file, 0, ","));
foreach ($row as $ix => $purchases) { 
  echo "<option value='" . $purchases . ">" . $purchases . "</option>";
  }

I know you’ve fixed it another way, but just so you can see another option.

Using a file to populate your list box brings forward another problem - what happens when your site has more than one user? It’s an unusual approach - what sits between the first bit of code that writes the file, and the last bit of code that reads it back in and creates a list box from it? So many other ways, such as session variables, seem better options.

Thanx for the alternative gives me a broader vision how to approach

I have still one small problem not sure what is wrong when using First Name space Last Name only First Name get save it looks like after space everything is omitted

Nancy Skosana get saved as Nancy

Using what code?

<?php   
    $file = fopen("purchases.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $purchases = $row[0];

    echo "<option value='".$purchases."'>" . $purchases. "</option>";

    }   
?>

Is the correct information being written to the file and read wrongly, or is it going into the file wrongly? What’s in the file? What if you do something like:

$purchases = $row[0] . " " . $row[1];

Also, if the option value is the same as the text you display inside the <option> </option> tags, there’s no need to put it in. You only need to specify the value when it’s different:

<option value="14">Brake adjuster</option>

When I inspect in google chrome it shows

<option value="Nancy" skosana="">Nancy Skosana</option>

and when saves to file it save as

Nancy

going into the file wrongly

also tried your suggestion still the same

$purchases = $row[0] . " " . $row[1];

Well, it will do. Look at what you have in the value field - just the first name, so that’s all that is submitted in the $_POST array. You need to put both in there, as you do in the description, or leave the value out altogether as I mentioned above. You seem to be putting the last name in as a separate parameter for some reason.

That would only work if both names were going into the file, but comma-separated.