Jump to content

Increase Value In Database Every 5 Seconds


oliverj777

Recommended Posts

Hello,

 

I'm working on a simple project so bear with me, I have a variable called 'wood' on my SQL database, and I want to somehow increase the 'wood' value (int) every 5 seconds... Even when the user is not currently view that page.

 

So what I'm thinking is saving the current mktime() to my database once - for sake of argument, this is going to be called 'time_cache', as well as the wood(int) variable.

 

Then, when the user logs in to see how much wood there is, the PHP will, take the mktime() and subtract the 'cache_time' to see how many seconds have passed... Now I want to increase the wood int by 1 every 5 seconds that have passed.

 

So I guess I need to do a division such as $seconds_passed = (mktime() - $time_cache) / 5. Which will give me the amount of 5 seconds that have passed, right?

 

Then, surely my $wood will just equal $seconds_passed?

 

I'm not sure if this is the best way to do something like this? Any one else have an idea? Also, my wood value doesn't 100% work as it give a decimal... I just want the wood value to increase by 1, every 5 seconds.

 

floor will remove the decimal places in this case.

 

Okay, so this works nicely... My wood increases by 1 every 5 seconds, one major floor though (sorry for the pun ;D), is that my code forces my wood to be fixed, by that I mean that I can't subtract wood due to it being measured in my cached time, and relative time.

 

What I was hoping for, was something like my wood has a default value of, say 20 on my database and my 'time setup' would just add 1 every 5 seconds to that value... Instead, what happening is that my time setup IS my wood value.

 

Any ideas?

Would really appreciate it, thanks

My wood increases by 1 every 5 seconds

 

The ladies must love you. :P

 

*cough*

 

Sorry...

 

Anyway, if you want to run code on specific time intervals without a user executing it (viewing the page) then you'll have to use something like a cron job.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.