Reputation: 1
First of all I want you to know that my knowledge about coding is very very low, almost 0. I have done some coding using tutorials, but now I am stuck :(
I have this code:
$fp = fopen('myfile.txt', 'w');
fwrite($fp, "Kilometrii : $a. Consum : $b. Pret : $c.Cost : $num4.\");
fclose($fp);
This code is doing some calculation and then it saves the results to a text file. My problem is that it saves only one result; if I do a second calculation it overwrites the first result and so on. My second problem is that the result is saved on a single line.
Example:
(Kilometrii : -88687. Consum : -148.06115890717. Pret : 0.00024164007585046.Cost : 31.73.\ ).
I want to show the results one above the other and separate multiple calculations by a bigger space. Please help me! Thank you!
Upvotes: 0
Views: 70
Reputation: 79024
You need to append with a
and you have an errant backslash \
, but you can use it to add a newline \n
:
$fp = fopen('myfile.txt', 'a');
fwrite($fp, "Kilometrii : $a. Consum : $b. Pret : $c.Cost : $num4.\n");
fclose($fp);
If viewing in a browser you'll need a <br>
instead or run it through nl2br()
.
Upvotes: 1
Reputation: 15311
You are opening the with the w
flag which truncates the file to zero length. You probably want to open it with a
(append mode) to not erase the file and put the cursor at the end of the file. You can see all the modes on the fopen manual page
Example:
$fp = fopen('myfile.txt', 'a');
fwrite($fp, "Kilometrii : $a. Consum : $b. Pret : $c.Cost : $num4.\n");
fclose($fp);
Upvotes: 1
Reputation: 1
Try this, fopen('myfile.txt', 'a') instead of 'w'. Including a link for further reference http://php.net/manual/en/function.fopen.php
Upvotes: 0