awilderbeast
asked on
generate random number once a week
hi all as the title suggests i want a script that will generator a random number between 1 and 100 every friday
i know to get the number i use print rand(1, 100);
but how to get it to run once a week on friday?
THanks
i know to get the number i use print rand(1, 100);
but how to get it to run once a week on friday?
THanks
ASKER
ive changed the path of the file to
$file= $_SERVER['DOCUMENT_ROOT']. '/admin/ra nd_num.txt '; // file for temporary storage of random number
and im just getting blank page
will it create a number if there is no number present?
$file= $_SERVER['DOCUMENT_ROOT'].
and im just getting blank page
will it create a number if there is no number present?
The most common and practical way of doing so is setting Cron job for script which generating your number.
To run script at let's say 8AM each Friday you'll need to setup cron like this:
m h dom mon dow command
00 08 * * 5 yourscript
m - minute
h - hour
dom - day of month
mon - month
dow - day of week
wildcards mean any.
You can input this string either in CPanel or if have shell access to server by calling command
[b]crontab -e[/b]
and pasting following string "00 08 * * 5 yourscript"
And in script you can execute code which will write randomly generated number to external file which could be used by other files.
Here is more details about crontab: http://www.adminschoice.com/docs/crontab.htm
About Cpanel Cron Job editor: http://www.2serveu.net/cpanel-tutorials/standardcron.html
Of course you can check for Friday internally in script, but to do this you need to be sure that it would be accessed at least daily.
To run script at let's say 8AM each Friday you'll need to setup cron like this:
m h dom mon dow command
00 08 * * 5 yourscript
m - minute
h - hour
dom - day of month
mon - month
dow - day of week
wildcards mean any.
You can input this string either in CPanel or if have shell access to server by calling command
[b]crontab -e[/b]
and pasting following string "00 08 * * 5 yourscript"
And in script you can execute code which will write randomly generated number to external file which could be used by other files.
Here is more details about crontab: http://www.adminschoice.com/docs/crontab.htm
About Cpanel Cron Job editor: http://www.2serveu.net/cpanel-tutorials/standardcron.html
Of course you can check for Friday internally in script, but to do this you need to be sure that it would be accessed at least daily.
<?php
$randNumber=rand(1, 100);
$file = "test_files/number.txt";
$fh = fopen($file, 'w') or die("file couldn't be opened");
fwrite($fh, $randNumber);
fclose($fh);
?>
ASKER
i dont have the level of access to teh web server to install and use cron though :|
You don't need to install it, as it's already on server. And access to cron is usually done via some sort of control panel on hosting.
If not you have to check for day of week in a way like suggested by NerdsOfTech.
If not you have to check for day of week in a way like suggested by NerdsOfTech.
ASKER
my hosts is with a local business, i dont have a control panel
NerdsOfTechs method didnt work either :|
it didnt write any number to the file
do i need chmod settings?
NerdsOfTechs method didnt work either :|
it didnt write any number to the file
do i need chmod settings?
I see.
NerdsOfTechs code is working, I've just checked it. To make it work you need to create empty file, chmod it for writing and edit it timestamp so it would be at least 7 day old.
You also need to change current day of the week to Thursday(if today is Thursday in your timezone), like this:
if ($today['weekday'] == 'Friday'){ // today is friday so do update
to
[b]if ($today['weekday'] == 'Thursday'){ [/b]
In Linux your actions in console would look like in code snippet.
But you need to make sure that this script would be called each friday, if not it wouldn't work... You can modify it to rely only on timestamp check. But it all depends from what you wish to achieve, if you give more details I can try to help you.
NerdsOfTechs code is working, I've just checked it. To make it work you need to create empty file, chmod it for writing and edit it timestamp so it would be at least 7 day old.
You also need to change current day of the week to Thursday(if today is Thursday in your timezone), like this:
if ($today['weekday'] == 'Friday'){ // today is friday so do update
to
[b]if ($today['weekday'] == 'Thursday'){ [/b]
In Linux your actions in console would look like in code snippet.
But you need to make sure that this script would be called each friday, if not it wouldn't work... You can modify it to rely only on timestamp check. But it all depends from what you wish to achieve, if you give more details I can try to help you.
> rand_num.txt #This is to create empty file rand_num.txt
chmod 666 rand_num.txt #chmoding it to writeable by everyone
touch 01010000 rand_num.txt #changing timestamp to 1St January 2009
ASKER
is there anyway i could do it to a mysql db instead of a text file
i have created a field called random numer in the tblAdmin
thanks
i have created a field called random numer in the tblAdmin
thanks
Sure just try something like this. it will insert
<?php
mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("yourDBhere") or die(mysql_error());
//This would insert into table settings, row randNumber with it's current value.
mysql_query("INSERT INTO settings
(name, value) VALUES('randNumber', $randNumber ) ")
or die(mysql_error());
?>
ASKER
yeah i know how to add info to mysql
but how would i then get the value in the mysql db changed everyweek?
would it be easier to do it by a button?
but how would i then get the value in the mysql db changed everyweek?
would it be easier to do it by a button?
Hey awilderbeat its NOT FRIDAY YET ;)
// comment lines
// if ($today['weekday'] == 'Friday'){ // today is friday so do update
// -- and --
//}
Yes you need 777 I believe
Note: this solution will write one file per folder that it executes in.
Debug: outputs the random number
// comment lines
// if ($today['weekday'] == 'Friday'){ // today is friday so do update
// -- and --
//}
Yes you need 777 I believe
Note: this solution will write one file per folder that it executes in.
Debug: outputs the random number
<?php
$file='rand_num.txt'; // file for temporary storage of random number
$update=0; // flag to update file
if (file_exists($file))
{
$startdate = filemtime($file);
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$fil = fopen($file, 'r');
$num = fread($fil, filesize($file));
fclose($fil);
}
}
else
{
$update=1; // file does not exist update flagged
}
if ($update){
$today = getdate();
if ($today['weekday'] == 'Friday'){ // today is friday so do update
$fil = fopen($file, 'w');
$num = rand(1, 100);
fwrite($fil, $num);
fclose($fil);
}
}
echo $num;
?>
ASKER
yeah i changed it to thursday in the code and its becuase i dont have enough permisson to chmod
could your code be adapated to put the random number in a mysql field instead?
Thakns
could your code be adapated to put the random number in a mysql field instead?
Thakns
LOL. You said fridays
Here how about I make it run for the first time no matter what day it is :)
=NerdsOfTech
Here how about I make it run for the first time no matter what day it is :)
=NerdsOfTech
<?php
$file='rand_num.txt'; // file for temporary storage of random number
$update=0; // flag to update file
$forceupdate=0; // flag to force update
if (file_exists($file))
{
$startdate = filemtime($file);
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$fil = fopen($file, 'r');
$num = fread($fil, filesize($file));
fclose($fil);
}
}
else
{
$update=1; // file does not exist update flagged
$forceupdate=1; // file does not exist force update flagged
}
if ($update){
$today = getdate();
if (($today['weekday'] == 'Friday') || $forceupdate){
// today is friday or is forced so do update
$fil = fopen($file, 'w');
$num = rand(1, 100);
fwrite($fil, $num);
fclose($fil);
}
}
echo $num;
?>
Sure it could.
ASKER
could you show me for the mysql instead, i dont have permisson to change chmod settings from my ftp client
so the text doc thing wont work for me :(
so the text doc thing wont work for me :(
Do you have an existing table for this?
ASKER
yes
tblAdmin
field `Rand_Num`
thanks
tblAdmin
field `Rand_Num`
thanks
add another field for date/time please
ASKER
Rand_Num_Date added varchar 255
thanks
thanks
do you want it to keep record of numbers selected or a replace strategy.
Thanks for the quick updates
Thanks for the quick updates
ASKER
just replace it every week, only once every friday though if thats possible?
Thanks
Thanks
Thanks again. Alright last questions.
Does it matter if I use Rand_Num_Date to write UNIX date format (non-date)?
E.G. is it required that the Rand_Num_Date be human readable DD/MM/YYYY or just UNIX ##########?
Thanks
Does it matter if I use Rand_Num_Date to write UNIX date format (non-date)?
E.G. is it required that the Rand_Num_Date be human readable DD/MM/YYYY or just UNIX ##########?
Thanks
ASKER
if its not too much trouble human readable, if it is doesnt matter, i can just type it in
thankyou very much for your assistance thus far
thankyou very much for your assistance thus far
Here is the UNIX (faster) style solution >:)
=NerdsOfTech
=NerdsOfTech
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
// INSERT TABLE CONNECTION CODE HERE
// READ DATA
$results = mysql_query(SELECT Rand_Num, Rand_Num_Date FROM tblAdmin LIMIT 1);
if (mysql_numrows($result) != 0)
{
$row = mysql_fetch_array($result)
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num_Date'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query('INSERT INTO tblAdmin (Rand_Num, Rand_Num_Date) VALUES ("' . $num . '", "' . time() . '");');
}elseif ($today['weekday'] == 'Friday'){
// today is friday or is forced so do update
mysql_query('UPDATE tblAdmin SET Rand_Num = "' . $num . '", Rand_Num_Date = "' . time() . '");');
}
}
echo $num;
?>
Oops dont use that code wait for my correction!
Syntax error corrected :)
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
// INSERT TABLE CONNECTION CODE HERE
// READ DATA
$results = mysql_query('SELECT Rand_Num, Rand_Num_Date FROM tblAdmin LIMIT 1;');
if (mysql_numrows($result) != 0)
{
$row = mysql_fetch_array($result)
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num_Date'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query('INSERT INTO tblAdmin (Rand_Num, Rand_Num_Date) VALUES ("' . $num . '", "' . time() . '");');
}elseif ($today['weekday'] == 'Friday'){
// today is friday or is forced so do update
mysql_query('UPDATE tblAdmin SET Rand_Num = "' . $num . '", Rand_Num_Date = "' . time() . '");');
}
}
echo $num;
?>
Correction:
Logic error: existing row $num = $row['Rand_Num];
Logic error: existing row $num = $row['Rand_Num];
Here is the corrected code.
Also one more note if Rand_Num is a numbertype then remove the quotes from the SQL statements refering to Rand_Num
Mission complete
=NerdsOfTech
Also one more note if Rand_Num is a numbertype then remove the quotes from the SQL statements refering to Rand_Num
Mission complete
=NerdsOfTech
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
// INSERT TABLE CONNECTION CODE HERE
// READ DATA
$results = mysql_query('SELECT Rand_Num, Rand_Num_Date FROM tblAdmin LIMIT 1;');
if (mysql_numrows($result) != 0)
{
$row = mysql_fetch_array($result)
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query('INSERT INTO tblAdmin (Rand_Num, Rand_Num_Date) VALUES ("' . $num . '", "' . time() . '");');
}elseif ($today['weekday'] == 'Friday'){
// today is friday or is forced so do update
mysql_query('UPDATE tblAdmin SET Rand_Num = "' . $num . '", Rand_Num_Date = "' . time() . '");');
}
}
echo $num;
?>
ASKER
it doesnt work :S
ive added my db connection,
i spotted two errors,
the results query said results but then was defined lower as result without s
and changed mysql_numrows to mysql_num_rows
also the table is not LuckyNo becuase tblAdmin has multiple fields in
can you tell me whats wrong?
i thought it migh tbe
// WRITE DATA
if ($update){
needing to be
// WRITE DATA
if ($update == 1){
but didnt work
Thanks
ive added my db connection,
i spotted two errors,
the results query said results but then was defined lower as result without s
and changed mysql_numrows to mysql_num_rows
also the table is not LuckyNo becuase tblAdmin has multiple fields in
can you tell me whats wrong?
i thought it migh tbe
// WRITE DATA
if ($update){
needing to be
// WRITE DATA
if ($update == 1){
but didnt work
Thanks
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php');
// READ DATA
$result = mysql_query('SELECT Rand_Num, Rand_Num_Date FROM tblLuckyNo LIMIT 1') or die(mysql_error());
if (mysql_num_rows($result) != 0)
{
$row = mysql_fetch_array($result)
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query('INSERT INTO tblLuckyNo (Rand_Num, Rand_Num_Date) VALUES ("' . $num . '", "' . time() . '");');
}elseif ($today['weekday'] == 'Thursday'){
// today is friday or is forced so do update
mysql_query('UPDATE tblLuckyNo SET Rand_Num = "' . $num . '", Rand_Num_Date = "' . time() . '");');
}
}
echo $num;
?>
ASKER
i got it to work :)
there was some ; missing and i changed ($insert ) to ($insert == "1") and ($update ) to ($update == "1")
now the thing is everytime someone refreshes the page on that day it gives a new number, i only want it to happen once per day, is that possible?
p.s changed to thursday to test it, i still want friday lol
there was some ; missing and i changed ($insert ) to ($insert == "1") and ($update ) to ($update == "1")
now the thing is everytime someone refreshes the page on that day it gives a new number, i only want it to happen once per day, is that possible?
p.s changed to thursday to test it, i still want friday lol
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php');
// READ DATA
$result = mysql_query("SELECT Rand_Num, Rand_Num_Date FROM tblLuckyNo LIMIT 1") or die(mysql_error());
if (mysql_num_rows($result) != 0)
{
$row = mysql_fetch_array($result);
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = ($nowdate - $startdate) / (60*60*24);
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update == "1"){
$today = getdate();
$num = rand(1,100);
if ($insert == "1"){ // runs instead if insert is flagged
mysql_query("INSERT INTO tblLuckyNo (Rand_Num, Rand_Num_Date) VALUES ('$num', '".time()."')") or die(mysql_error());
}elseif ($today['weekday'] == 'Thursday'){
// today is friday or is forced so do update
mysql_query("UPDATE tblLuckyNo SET Rand_Num ='$num', Rand_Num_Date='".time()."')") or die(mysql_error());
}
}
echo $num;
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
indeed it does :D and now it will change the next week?
Affirmative. On friday or whenever the script is run after 7 days from the last update to record
so it is pseduo-automatic :)
=NerdsOfTech
so it is pseduo-automatic :)
=NerdsOfTech
ASKER
thankyou very much :D
yoru a genius
seeing your php skills, could you help me with my other post here
https://www.experts-exchange.com/questions/24266525/error-checking-php-isnt-working.html
Thanks again :)
yoru a genius
seeing your php skills, could you help me with my other post here
https://www.experts-exchange.com/questions/24266525/error-checking-php-isnt-working.html
Thanks again :)
ASKER
this guy knows his stuff :)
168 hour minimum before update AND update only on Friday
Since the page must "run" for this update to occur I suggest you create a scheduled task for each Friday to load this page.
Also I added the floor function to $days to give you a rounded down integer value so that you can update on FRIDAY ONLY at anytime during that day.
Also I added the floor function to $days to give you a rounded down integer value so that you can update on FRIDAY ONLY at anytime during that day.
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php');
// READ DATA
$result = mysql_query("SELECT Rand_Num, Rand_Num_Date FROM tblLuckyNo LIMIT 1") or die(mysql_error());
if (mysql_num_rows($result) != 0)
{
$row = mysql_fetch_array($result);
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = floor (($nowdate - $startdate) / (60*60*24)) // round down to nearest int;
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query("INSERT INTO tblLuckyNo (Rand_Num, Rand_Num_Date) VALUES ('$num', '".time()."')") or die(mysql_error());
}elseif ($today['weekday'] == 'Friday'){
// today is the day to update
mysql_query("UPDATE tblLuckyNo SET Rand_Num ='$num', Rand_Num_Date='".time()."')") or die(mysql_error());
}
}
echo $num;
?>
Corrected:
<?php
$update=0; // flag to update number
$insert=0; // flag to update number via insert
$num=0; // random number result
include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php');
// READ DATA
$result = mysql_query("SELECT Rand_Num, Rand_Num_Date FROM tblLuckyNo LIMIT 1") or die(mysql_error());
if (mysql_num_rows($result) != 0)
{
$row = mysql_fetch_array($result);
$startdate = $row['Rand_Num_Date'];
$nowdate = time();
$days = floor (($nowdate - $startdate) / (60*60*24)); // round down to nearest int;
if ($days >= 7){
$update=1; // more than a week has passed update flagged
}else{
$num = $row['Rand_Num'];
}
}
else
{
$update=1; // data does not exist update flagged
$insert=1; // data does not exist insert flagged
}
// WRITE DATA
if ($update){
$today = getdate();
$num = rand(1,100);
if ($insert){ // runs instead if insert is flagged
mysql_query("INSERT INTO tblLuckyNo (Rand_Num, Rand_Num_Date) VALUES ('$num', '".time()."')") or die(mysql_error());
}elseif ($today['weekday'] == 'Friday'){
// today is the day to update
mysql_query("UPDATE tblLuckyNo SET Rand_Num ='$num', Rand_Num_Date='".time()."')") or die(mysql_error());
}
}
echo $num;
?>
ASKER
if i include this page in another thats visted everyday will that be ok to make it work?
Yes. As long as it runs at least once this will work.
Open in new window