Add random number per day to an amount which never go less than its previous value.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vivekneo
    New Member
    • Sep 2010
    • 18

    Add random number per day to an amount which never go less than its previous value.

    Hello,

    I am trying to make a fake product sales value counter, which will update it self to certain amount not more than $100 per day i.e in 24hrs.

    To make this even simple here an Ex:
    Code:
    <?php
    	$amt = 100000;
    	srand(floor(time() / (60*60*24)));
    	$total = $amt + rand() % 100;
    	echo "$". $total ."/-";
    	$amt = $total;
    ?>
    How ever the code above causes to roll back or reduce to previous days value.

    day1 = 100087
    day2 = 100083 .this is the issue.

    it should be 100087 + 83 = 100170
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    That code hard-codes the $amt value to 100000 on every run. If you want it to increase on yesterday's value, you need to store it somehow.

    The simplest way would be to dump it into a text file on every run and read it back the next run. Check out the file_get_conten ts and file_put_conten ts functions for that.

    For example, modifying your previous example:
    Code:
    <?php
        // The location of the file to use to store 
        // the previous amount
        $dataFile = "amt.txt";
        
        // If the file doesn't exist, default to 
        // the inital 100000.
        if (!file_exists($dataFile)) {
            $amt = 100000;
        }
        else {
            // Otherwise read the previous value from
            // the file.
            $amt = (int) file_get_contents($dataFile);
        }
        
        // Generate the new value...
        srand(floor(time() / (60*60*24)));
        $total = $amt + rand() % 100;
        echo "$". $total ."/-";
        
        // And dump it back into the file.
        if (!file_put_contents($dataFile, $total)) {
            // If it fails to write to the fle, you'll 
            // want to know about it...
            echo "Failed to save the new total!";
        }
    ?>
    That's about as simple as it gets.

    Comment

    • Vivekneo
      New Member
      • Sep 2010
      • 18

      #3
      Hello Atli,
      Thank you for your reply, is their any other way then using files, also the value stored into the file is over written very time.

      I am fare new to files and php, yet i will try to run your solution.

      Comment

      • Vivekneo
        New Member
        • Sep 2010
        • 18

        #4
        Atli,
        the above code generates random value after every page refresh, how do i correct the code so that the addition happens once 24hrs, and i am okay to save the value into the file.

        Please advice.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          You'll either have to save the time of the last update with the value, so that you can compare it to the current time when before you updated it, or you can just use the filemtime() function you can get a timestamp for when the file was last modified. You can then use that to find out if a day has passed, and if not exit the script.

          Fox example:
          Code:
          // Subtract the timestamp 24 hours ago from the
          // last time the file was edited, to get the
          // number of seconds still remaining until the
          // file can be edited again.
          $seconds = filemtime($dataFile) - strtotime("now - 24 hours");
          
          // If the seconds are more than 0, there is
          // still time remaining until it can be edited.
          if ($seconds > 0) {
              echo "You can only update the file once every 24 hours. ";
              echo "Time until next update: ";
              echo date("H:i:s", $seconds );
              exit;
          }

          Comment

          Working...