Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    automatically update, even if users are not online

    I would like to implement this code below:

    PHP Code:
    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 Code:
    <?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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    Code:
    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.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •