...

View Full Version : automatically update, even if users are not online



Dan13071992
11-26-2011, 12:27 AM
I would like to implement this code below:


if (strip_tags($_POST['production']) == "Yes"){
$production=$_POST['production'];
mysql_query("UPDATE bf SET stock=stock+0 WHERE producing='$production'");
mysql_query("UPDATE bf SET stock=stock+1000 WHERE _countryid='$fetch->_countryid'");
mysql_query("UPDATE users SET money=money-1000 WHERE username='$fetch->username'");


into my inc-functions.php file:


<?php

function optCountries($sel) {
$result='';
$query=mysql_query("SELECT * FROM countries ORDER BY country ASC");
while ($row=mysql_fetch_assoc($query)) {
$result.='<option value="'.$row['id'].'"'.($sel==$row['id'] ? ' selected' : '').'>'.$row['country'].'</option>';
}
return $result;
}

function makePassword($len) {
$pass='';
$allowed='Taken Out For Security';
for ($i=1;$i<=$len;++$i) {
$pass.=$allowed[mt_rand(0,strlen($allowed))];
}
return $pass;
}

function generateCode() {
$valid='Taken Out For Security';
$strCode='';
for ($i=0;$i<10;++$i) {
$strCode.=$valid[mt_rand(0,strlen($valid))];
}
return $strCode;
}

function dbSafe($txt) {
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {$txt=stripslashes($txt);}
return mysql_real_escape_string($txt);
}


function makecomma($input)

{



if(strlen($input)<=3)

{ return $input; }

$length=substr($input,0,strlen($input)-3);

$formatted_input = makecomma($length).",".substr($input,-3);

return $formatted_input;

}


?>

The idea is so that if in the bf the status is set to 'producting=yes' then every hour on the hour it adds 1000 to the stock in each bf, however I am wondering if this is possible to do without anyone logged into the site, as inc-functions is the sites functions as you can see, i also have inc-userfunctions.php which is to update users online sessions ect.

Is this possible to do, if so how?

Thanks.

Dan

mlseim
11-26-2011, 12:38 AM
If you think about it ...
If nobody is visiting the site, nobody can see it increment.
(like ... "does a falling tree make a noise in the forest with nobody around" idea).

So, you have a script that is executed everytime someone
visits or refreshes the page. It has saved a timestamp from
the last time it updated. It just sits there until someone visits
and checks its timestamp against the current time. It adds
the correct amount of $1000 to the stocks as needed at that
instant, just before it redirects to the page for the visitor to see.
If 3 hours have past since the page was visited, it adds $3000, etc.

Dan13071992
11-26-2011, 12:57 AM
Well the whole point of it is that every hour it updates, regardless of if people are online or not, this is because every hour bullets are ment to be re stocked if the bf is in producing mode

Fou-Lu
11-26-2011, 05:01 AM
You're missing the idea though. It is a waste to update anything when there is no reason to do so. It's trivial to update a block chunk (ie: 6 hours from the last load, so force 6*x for an increment) and update when a request is pushed for the actual data. Without anyone to interact with it, I don't see a reason to force it to update.
For an actual answer though, there are two ways. One is by using a cron job (beyond the scope of PHP itself), and the other is to write an infinite service in PHP that sleeps for 60 minutes after each execute.
The idea of not updating at all and waiting for the next request is commonly known as poor man's cron. Back in the day, cron was actually a premium option, so we wrote code that was designed to execute at specific times, but would only do so when activity was occurring, and sweep up anything missing.

Dan13071992
11-26-2011, 03:02 PM
I see your point now, sorry I was tired yesterday when I read what you said, how could i implement what you are saying, into my inc-functions.php file?

Fou-Lu
11-26-2011, 03:30 PM
You can do this mostly in SQL. Assuming MySQL, you can use a combination of time_diff and hour functions to calculate.
All you need to know is the last datetime something was written. Then you can use SQL to calculate the number of hours, something easy:


UPDATE `table` SET `avalue`=`avalue`+ (1000 * HOUR(TIME_DIFF(NOW(), `lastupdate`))), `lastupdate`= NOW() WHERE acondition

TIME_DIFF will calculate the difference in a time (or datetime) and return it expressed as a time, and hour will extract the number of hours from within that time. So if the difference is 46:23:14, it will result in 46 * 1000. Then it updates the lastupdate with the new datetime. lastupdate has to be of type datetime.

mlseim
11-26-2011, 05:46 PM
And also ... if your webhost server is not in the same timezone as you, or the
timezone you wish to use, you can correct that also. Test it first by:

echo date("H:i:s", time());

See if the timezone is correct.

===========

As Fou-Lu was saying, add a new column to your table called lastupdate.
When I do this type of thing, I just use the UNIX timestamp with a VARCHAR(12) column.
I know there are MySQL date/time type of columns, but I still think it's easier to use
timestamp and work with it in my script. It's just a personal opinion. And I don't know
why I don't use INT(12) type ... I guess it's a habit to use VARCHAR. I'm sort of lazy.

The UNIX timestamp is actually a 10-digit integer, but in 2038, it will be an 11-digit.

Dan13071992
11-26-2011, 05:49 PM
its great for me, my host is hosted in the UK, so its the same timezone as me as thats where i live, thanks for all the advice, im going to get to work with it now, ill keep you guys updated, thanks for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum