How to use for loop to write to file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    How to use for loop to write to file?

    This has got to be an easy question - for some reason I can't seem to get
    this to work. Is there a better way? All I want is a quick and dirty way
    to populate a file with UNIX time stamps...

    <?php
    //viscount_helper .php
    $vc = 'viscounter_tes t.txt';
    $avdaily = 5; average daily
    $total_count = (365 * $avdaily); //time period = one year
    echo $total_count;
    $interval = (86400)/$avdaily; //evenly spaced intervals
    echo $interval;
    $visit = time() - (31536000);
    echo $visit;
    //$next_time = ($first_time + $interval)
    $fp = fopen($vc,"a");
    for($i = 1; $i > $total_count; $i++)
    {
    fwrite($fp, $visit."\n");
    $visit = $visit + ($interval * $i);
    }
    fclose($fp);


  • Ian.H

    #2
    Re: How to use for loop to write to file?

    On Tue, 20 Jul 2004 07:03:18 +0000, deko wrote:
    [color=blue]
    > This has got to be an easy question - for some reason I can't seem to get
    > this to work. Is there a better way? All I want is a quick and dirty way
    > to populate a file with UNIX time stamps...
    >
    > <?php
    > //viscount_helper .php
    > $vc = 'viscounter_tes t.txt';
    > $avdaily = 5; average daily
    > $total_count = (365 * $avdaily); //time period = one year
    > echo $total_count;
    > $interval = (86400)/$avdaily; //evenly spaced intervals
    > echo $interval;
    > $visit = time() - (31536000);
    > echo $visit;
    > //$next_time = ($first_time + $interval)
    > $fp = fopen($vc,"a");
    > for($i = 1; $i > $total_count; $i++)[/color]
    ^

    Are you sure this should be > rather than < ?

    Unless average daily is ever 0, the loop will never run =)

    [color=blue]
    > {
    > fwrite($fp, $visit."\n");
    > $visit = $visit + ($interval * $i);
    > }
    > fclose($fp);[/color]


    Other than that, I'd say it's pretty much the "only" way =)



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    • deko

      #3
      Re: How to use for loop to write to file?

      > Are you sure this should be > rather than < ?

      Good catch - that was part of it...

      Here's a working version:

      $vc = 'countfile.txt' ;
      $avdaily = 38; //expected average daily hits
      $total_count = (366 * $avdaily);
      echo "<br>total_coun t = ".$total_co unt;
      $interval = (86400 / $avdaily); //evenly spaced intervals
      echo "<br>interv al = ".$interval ;
      $visit = (time() - (31622400));
      echo "<br>first visit = ".$visit."<br>< br>";
      $fp = fopen($vc,"w");
      for($i = 1; $i <= $total_count; $i++)
      {
      echo $visit."<br>";
      fwrite($fp, $visit."\n");
      $visit = ($visit + $interval);
      }
      echo "<br>last visit = ".$visit;
      fclose($fp);

      The problem is that I am getting decimal output in $visit - e.g.
      1058691603.37
      Is there a way to force an integer value to avoid decimals?


      Comment

      • Andy Barfield

        #4
        Re: How to use for loop to write to file?

        deko wrote:[color=blue]
        > The problem is that I am getting decimal output in $visit - e.g.
        > 1058691603.37
        > Is there a way to force an integer value to avoid decimals?[/color]

        Get the integer value of a variable

        Comment

        • deko

          #5
          Re: How to use for loop to write to file?

          > http://www.php.net/intval

          that's the ticket


          Comment

          Working...