By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

Incrementing a variable by one loop

P: 2
Hi, nice to meet everyone, im Arekkusu and im new to the forums.

I have a slight problem..hopeing you guys and gals can help with. Its about trying to get a variable ($i), to increase by one every time a number of strings from the $POST array is written to a flat file. Does that make sense?

This what I got so far.

$racerDetails = $_POST;

//write to file//
$min = 0;
$max = 20;
$i = 0;

if($i >= $min)
if($i < $max)
$file = "Comments.txt";
$handle = fopen($file, "a");
fwrite($handle, $i.$racerDetails["Name"]." ".$racerDetails["Bike"]."\n");

So what should basically happen is that once the $_POST array values specified have been written to a flat file, it should increase the variable i by 1. After the next time data is written to the flat file, var i should equal 2, and so on and so on until it reaches 20.

I tried using a for loop but I couldnt work out how to stop it once it had incremented i by 1, so it just printed the same values to the flat file 20 times.

Any ideas?

If you need me to exlain anything just say, apologies as I'm not to great with getting my thoughts down on paper...or

Thanks in advance.

May 17 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 5,821
You'll need to store the value of your variable somewhere outside of the context of your script, either in a separate file, or in a database or in the User's session.

Every time PHP runs your script, the value of $i will be reset to 0.

You could use $_SESSION['writeCountOrWhateverYouWantToCallIt'], for example....
May 17 '07 #2

P: 2
Thanks man, the help is appreciated!
May 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.