0

I am currently writing to a .csv file in .php

In the following code below, I would like the chip ID's to be within the same cell, but separated by a line break between each value. To my knowledge, surrounding the values you would like with the line breaks in double quotes honors the line breaks while within the same cell. To that effect, I wrote the following with the backslash escape characters.

<?php>
    $FirstChipID = "\"" . $_POST["FirstChipID"] . " \n ";
    $SecondChipID =  $_POST["SecondChipID"] . " \n ";
    $ThirdChipID = $_POST["ThirdChipID"] . " \n ";
    $FourthChipID = $_POST["FourthChipID"] . "\n ";

    $file = fopen( "chipID.csv", "a");
    fwrite($file, $FirstChipID . $SecondChipID .  
    $ThirdChipID . $FourthChipID . "\"" . "\n");
    fclose($file);
?>

However, this results in the .csv file creating new rows for each instance of "\n" instead of containing all the values within one cell. I am at a loss as how to approach this and would appreciate some guidance. Where in my logic caused this strange behavior? Am I utilizing something incorrectly? I received the same behavior when combining all chip ID's to a single string with the supposed proper double quotation formatting.

Harry You
  • 87
  • 2
  • 3
  • 11
  • You already retained them – Your Common Sense Jul 23 '23 at 06:01
  • @YourCommonSense Could you clarify? As stated before, my resulting csv file separates each chip id to a separate row instead of containing them all in a single cell. – Harry You Jul 23 '23 at 06:08
  • I was unable to reproduce your issue with the example you provide in question, as it does not contain the input data. Also to demonstrate the issue it should be enough to have two data-points. Please [edit] your question so that example becomes reproducible, Cf. [mre]. The description of the error picture looks clear, if not even verbose, to me. – hakre Jul 23 '23 at 06:28
  • Can't reproduce the problem. Using your code, it generates a file with the `\n`s in them in the right places. If you read it in a text editor, or import to Excel, it shows up as you'd expect. Precisely what are you doing in order to view the results, which is showing the text on separate lines? – ADyson Jul 23 '23 at 08:28

0 Answers0