number increment problem help required pleeease.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
g.mcgregor
Forum Newbie
Posts: 2
Joined: Tue Feb 24, 2009 5:22 pm

number increment problem help required pleeease.

Post by g.mcgregor »

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
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: number increment problem help required pleeease.

Post by Randwulf »

Yes, it can be done, though it's somewhat ugly.

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");
 
P.S. Depending on your server, you may need to remove '..' and '.' from $files.
g.mcgregor
Forum Newbie
Posts: 2
Joined: Tue Feb 24, 2009 5:22 pm

Re: number increment problem help required pleeease.

Post by g.mcgregor »

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
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: number increment problem help required pleeease.

Post by Randwulf »

No, that was my bad. The 'for' on line 4 should be 'foreach'.
Post Reply