473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $amt = 100000;
  3.     srand(floor(time() / (60*60*24)));
  4.     $total = $amt + rand() % 100;
  5.     echo "$". $total ."/-";
  6.     $amt = $total;
  7. ?>
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
May 30 '12 #1
4 2886
Atli
5,058 Expert 4TB
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_contents and file_put_contents functions for that.

For example, modifying your previous example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     // The location of the file to use to store 
  3.     // the previous amount
  4.     $dataFile = "amt.txt";
  5.  
  6.     // If the file doesn't exist, default to 
  7.     // the inital 100000.
  8.     if (!file_exists($dataFile)) {
  9.         $amt = 100000;
  10.     }
  11.     else {
  12.         // Otherwise read the previous value from
  13.         // the file.
  14.         $amt = (int) file_get_contents($dataFile);
  15.     }
  16.  
  17.     // Generate the new value...
  18.     srand(floor(time() / (60*60*24)));
  19.     $total = $amt + rand() % 100;
  20.     echo "$". $total ."/-";
  21.  
  22.     // And dump it back into the file.
  23.     if (!file_put_contents($dataFile, $total)) {
  24.         // If it fails to write to the fle, you'll 
  25.         // want to know about it...
  26.         echo "Failed to save the new total!";
  27.     }
  28. ?>
  29.  
That's about as simple as it gets.
May 30 '12 #2
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.
May 30 '12 #3
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.
May 30 '12 #4
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. // Subtract the timestamp 24 hours ago from the
  2. // last time the file was edited, to get the
  3. // number of seconds still remaining until the
  4. // file can be edited again.
  5. $seconds = filemtime($dataFile) - strtotime("now - 24 hours");
  6.  
  7. // If the seconds are more than 0, there is
  8. // still time remaining until it can be edited.
  9. if ($seconds > 0) {
  10.     echo "You can only update the file once every 24 hours. ";
  11.     echo "Time until next update: ";
  12.     echo date("H:i:s", $seconds );
  13.     exit;
  14. }
  15.  
May 30 '12 #5

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

Similar topics

10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
6
by: VAL | last post by:
How can I make an application form that generates a unique number starting from 1001.?
8
by: Locky | last post by:
Sorry if this is a repost - my connection went as I was posting the 1st time! I want to insert a random number, in mod 10, in a field, based on whether another field is empty or not. I am...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
15
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe ...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
0
by: urkel | last post by:
Hello everybody, I have these errors on C program that may be because of random number definition. I use an SSH to compile C like in Linux. May be you have idea whether the errors are due to the...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.