Incrementing a variable by one loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arekkusu
    New Member
    • May 2007
    • 2

    Incrementing a variable by one loop

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

    I have a slight problem..hopein g 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.

    [PHP]
    $racerDetails = $_POST;

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




    if($i >= $min)
    {
    if($i < $max)
    {
    $file = "Comments.t xt";
    $handle = fopen($file, "a");
    fwrite($handle, $i.$racerDetail s["Name"]." ".$racerDet ails["Bike"]."\n");
    fclose($handle) ;
    $i++;
    }
    }
    [/PHP]


    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 forum..lol.

    Thanks in advance.

    Arekkusu.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    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['writeCountOrWh ateverYouWantTo CallIt'], for example....

    Comment

    • Arekkusu
      New Member
      • May 2007
      • 2

      #3
      Thanks man, the help is appreciated!

      Comment

      Working...