I'm trying to use FOPEN to write to a file. I have a problem though.
I'm trying to write a series of items to a file. I'm writing to the file in a function i've created... for each item I need it to write a line to the file it has created.
For example: I have 5 items it's going to loop through.
It should
a) Open the file
b) Add EACH ITEM to the file
c) close the file.
Then, the next time I run my script, I want it to clear all items from the file, run from the top again.
Right now, I can get it to do one or the other. Either, it adds all my items, or it only adds the first one, depending on my order of operations.
Can someone assist me with the type of file open I need and perhaps a little order of operations?
My code looks like this (and will write all the data, but keeps appending and will not 'erase and start over' every time i open):
function create($dataname){
$datafile = "datafile.php";
$fh = fopen($datafile, "a") or die("can't open file");
#runs some code
if($item>1)
{
$dataname = "text item 1";
$fwrite($fh, $dataname);
}
elseif($item==0)
{
$dataname = "text item 2";
$fwrite($fh, $dataname);
}
else
{
$dataname = "text item 3";
$fwrite($fh, $dataname);
}
See the problem?