So I'm trying to append a string at the end of a row. The row and the string is inputted by the user.
//irrelevant...
$row = $_POST["whichRow"];
$Comment = $_POST["stringValue"];
$lines = array();
$arrayWithComment = array(";", "$actualComment");
$lineCount = 0;
foreach(file('/var/www/html/bkbpNew/tickets.csv') as $line){
if ($lineCount == $row){
array_push($lines, $line, $arrayWithComment[0], $arrayWithComment[1]);}
else{
array_push($lines, $line);}
$lineCount++;
}
file_put_contents('/var/www/html/bkbpNew/tickets.csv', $lines);
And that sort of works, but not in the way that I want it to. Here is the edited file before/after & the desired state:
(Input values for this example are comment = "test" and row = 2)
1;example;example;example;example;example;example
2;example;example;example;example;example;example;test
3;example;example;example;example;example;example
1;example;example;example;example;example;example
2;example;example;example;example;example;example
3;example;example;example;example;example;example
1;example;example;example;example;example;example
2;example;example;example;example;example;example
;test3;example;example;example;example;example;example
it automatically goes into the next line (Desired result) but I don't want it to. How do I go about this? Cheers!
when you use file()
, newline characters are included at the end of the line, so when you're adding your new comment to the end of the line you're adding it after the new line character
I'm not a big fan of your approach to this, but as a quick and dirty solution use the FILE_IGNORE_NEW_LINES
flag for file(), and then manually re-add the newline characters for each line in your loop