Hello,
Not sure whether this is done on the PHP or MySQL side. What i would like to do is to have the numbers stored in database (MySQL) to change hourly according to a formula.
Is there such a function in PHP or MySQL? Any help/comment would be appreciated, thanks.
I have a table containing the numbers of items i'm having now, money and food etc.
The scenario is that the food is decaying at every hour and i'm earning some amt. of money at the same hour. Let us assume that i earn 10 unit of money and lose 1 unit of food per hour, how do i make the database to update the amount hourly itself.
There are several ways to approach this one. In most cases.. its best to do stuff like this with your scripting language not your DB. Not knowing anything about your situation, its hard to give you a clear direction to go.
I'm guessing this would be a number that would be different for each item. If so, in your DB, store the date added and the decay rate. Then just use the date() function in php to do some math and figure it out. Just remember, php stores datetime fields in the format of "YYYY-MM-DD hh:mm:ss".. using strtotime() on your varible from mysql should be a help. This will convert it to a php date that you can compare things on.
Thanks for the response so far..
And for DaButcher, yes, i'm currently in progress in developing an online tick-based game. I think we couldn't see the php source code for the other games right? We could only see the output html code if we view source, if i'm not mistaken..unless there's some sort of configuration error on the server side
rather than updating every hour or so... add a datetime field which you set everytime you write the information to the database.
Then, instead of automatically doing everyone every hour, you can wait until you make a read of the row, if the timestamp is greater than some threshold, then you run the formula based on the amount of time which has passed since the timestamp.
The only downside to this method is if you want to do something like calculate the total wealth or food supply for economic purposes.
Otherwise... on the linux side look into cron, and in the windows side, look into AT or the windows scheduler... all those options will allow you to schedule a PHP script to run using the command line interpretter at any times you like... using this method I would still suggest using the timestamp approach in case you miss an execution due to a power outage or an otherwise down system.
skiflyers way might work..
it's good in theory, but there might be some issues if it is some kind of attack/revenue game, with something increasing and/or decreasing each tick.
you can run a mysql command to update fields.
the mysql command can be run thru perl or php, via crontab.
Have read thr stuff like cron and wincron. Seems like its the best solution for my problem..Hope that there's web host supports cron jobs.
As i'm currently designing the game in windows enviroment, i might have to manually update the database with the formula..and Wincron doesn't available as freeware also
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.