Hi all,
I am new to php and new to this site so please excuse me if this post is in the wrong place or a silly question.
I have created a form, and save the data in a .txt file for backup. My question is, can I add an icremental number to entries in this .txt file, a reference number as such. I have trawled the net and have found many tutorials for counters etc but none suitable for what I need.
Hope someone can help.
Best regards
Graham
number increment problem help required pleeease.
Moderator: General Moderators
-
g.mcgregor
- Forum Newbie
- Posts: 2
- Joined: Tue Feb 24, 2009 5:22 pm
Re: number increment problem help required pleeease.
Yes, it can be done, though it's somewhat ugly.
P.S. Depending on your server, you may need to remove '..' and '.' from $files.
Code: Select all
$files = scandir("insert desired directory here");
$counter = 0;
for ($files as $number)
{
$number = str_replace(".txt","",$number);
if ($number > $counter) //This if statement ensures that the highest numbered file is saved in $counter
{
$counter = $number;
}
}
$counter++;
$handle = fopen("desired directory/$counter" . ".txt",'w');
fwrite($handle,"stuff to write to file");
-
g.mcgregor
- Forum Newbie
- Posts: 2
- Joined: Tue Feb 24, 2009 5:22 pm
Re: number increment problem help required pleeease.
Hi Randwulf, thanks for the quick reply.
comes up with an error though ...
Parse error: syntax error, unexpected T_AS, expecting ';' in /home/fizzywic/public_html/Graeme_form/entry_form.php on line 10
probably my fault, will keep at it.
Many thanks
Graham
comes up with an error though ...
Parse error: syntax error, unexpected T_AS, expecting ';' in /home/fizzywic/public_html/Graeme_form/entry_form.php on line 10
probably my fault, will keep at it.
Many thanks
Graham
Re: number increment problem help required pleeease.
No, that was my bad. The 'for' on line 4 should be 'foreach'.