PP-Stuart Posted September 25, 2007 Share Posted September 25, 2007 Hello, just goin through a book on how to program in php and for a site I am making I need to create a script that generates a random number every day. I have written this so far and unfortunately it is not giving me the results I need. <?php $rotate = true; $vardate = $_POST["textfield"]; //$vardate = date(j, time()); if ($rotate) { $currentday = date(j, time()); $test = rand(1,100); $rotate = false; } if ($currentday != $vardate) { $rotate = true; } echo ("today is $vardate, and the test date is $currentday, and the random number is $test"); ?> <form id="form1" name="form1" method="post" action="<?php echo $_SERVER[php_SELF] ?>"> <input type="text" name="textfield" /> <input type="submit" name="submitbutton" /> </form> basically, the issue is that when the dates do not equal, signaling a change in the date, I want the variable $test to output a random number. What ends up happening is that it spits out a random number, and then turns null on the next page refresh. I am pretty sure I am going about this completely incorrectly haha, but I am in the learning phase. Any help would be totally sweet, I appreciate it. Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 25, 2007 Share Posted September 25, 2007 Well you can't just refresh the page. For the php script to work properly you have to keep inputing the data information in the text field as it is $_POST. If that is the problem than you can put it into the $_GET or even better, $_SESSION. If that's not the problem, please clarify. I'm a bit confused. Quote Link to comment Share on other sites More sharing options...
PP-Stuart Posted September 25, 2007 Author Share Posted September 25, 2007 Ok I only had the form up as a way to test how the script reacts to the changing of the numbers. So here is exactly what I want it to do. I want the $test variable to be a random number, and have it stay that same number for one whole day. Upon changing of the date, I want it to change to another random number, but only once, and have it stick for another 24 hours. The two if statements were the best my primitive brain could come up with as a way to change $test based on a changing of the date, but it seems the variable does not stick. Does this make any sense lol? Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 25, 2007 Share Posted September 25, 2007 To stick the variable you need a file or a database to put in the information. I recommend a simple text file. Php would then go in and update the date information. Everytime a user would view the page, php would extract the information from the file and output it. Quote Link to comment Share on other sites More sharing options...
PP-Stuart Posted September 25, 2007 Author Share Posted September 25, 2007 ok I tried that route but there must be something I am missing as I am not getting the correct results. so for the below code, I want to enter a number to the form, and when I enter a different number I want the random number to change, but if I enter the same number it should stay the same... Here is what I have. <?php $rotate = true; $vardate = $_POST["textfield"]; touch("currentdate.txt"); $filename = "currentdate.txt"; //$vardate = date(j, time()); if ($rotate) { //if the date has changed, create a new random number to the variable $test ($cf = fopen($filename, "w")) or die("The file could not be opened, please check your directory permissions"); fwrite($cf, $vardate); fclose($cf); $cf = fopen($filename, "r"); $currentday = fread($cf, 3); fclose($cf); $test = rand(1,100); $rotate = false; } if ($currentday != $vardate) { $rotate = true; } echo ("today is $vardate, and the test date is $currentday, and the random number is $test"); ?> <form id="form1" name="form1" method="post" action="<?php echo $_SERVER[php_SELF] ?>"> <input type="text" name="textfield" /> <input type="submit" name="submitbutton" /> </form> it is live here if you want to see what it does http://www.stuartkrempin.com/dailyrotate2.php Quote Link to comment Share on other sites More sharing options...
PP-Stuart Posted September 25, 2007 Author Share Posted September 25, 2007 any ideas? ??? Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 25, 2007 Share Posted September 25, 2007 There is a far easier way since the random generator it's not really random. It's just some fancy math applied on a seeded number. Random generators simulate "randomness" using very fast changing seeds, like the microseconds on the clock, thus the value outputed changes every time the seed changes. If you supply a constant seed, the random generator gets stuck. So if you seed the random generator with the current date, voilá, problem solved. <?php $seed = date("d"); //get the day. srand($seed); //seeds the random generator with it. echo $rand(); //get the not so random number ?> That way the rand() function will output the same number until the day changes. You can also specify ranges in the rand() function, it won't affect the behavior through the day. Be aware though, that the number WILL repeat itself over the months (ie. 25th September will return the same number as the 25th of December). You can overcome that problem by appliying some extra math and dates to the seed. Like $seed = $day+$month+$year; Hope it helps, cheers. Quote Link to comment Share on other sites More sharing options...
PP-Stuart Posted September 25, 2007 Author Share Posted September 25, 2007 wow thank you I believe that is exactly what I am looking for, I will reprogram the whole bit tomorrow and post the results. very much appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.