PHP MySQL give something in time increments without cron job

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Opalelement
Forum Newbie
Posts: 2
Joined: Sun Aug 10, 2008 4:22 pm

PHP MySQL give something in time increments without cron job

Post by Opalelement »

I have a MySQL table shown as the following (there are more fields but remover for the purpose of illustrating):

Code: Select all

+----+-------------+--------+--------------+--------+--------------+
| id | username    | energy | energy_total | health | health_total |
+----+-------------+--------+--------------+--------+--------------+
|  1 | Opalelement |     10 |           10 |     10 |           10 |
|  2 | DougFresh   |     10 |           10 |     10 |           10 |
+----+-------------+--------+--------------+--------+--------------+
There are things you can do that take away energy, but I don't wnat to give it back all at once at any given time. What I wnat to do is maybe every 5 minutes after the last action that took some away, give one back until it is full.

My idea was to make a database structure like the following:

Code: Select all

+----+-------------+--------+--------------+-------------+--------+--------------+-------------+
| id | username    | energy | energy_total | energy_used | health | health_total | health_used |
+----+-------------+--------+--------------+-------------+--------+--------------+-------------+
|  1 | *********** |     10 |           10 |             |     10 |           10 |             |
|  2 | *********   |     10 |           10 |             |     10 |           10 |             |
+----+-------------+--------+--------------+-------------+--------+--------------+-------------+
*_used will be a unix timestamp value
Then use a query to select the *_used fields and do "time() - *_used"
Then divide the result by however long I want the increment to be
After doing that I will just do something like [ICODE]UPDATE users SET energy=energy+$add_energy[/ICODE] because if it hasn't been enough time the $add_energy will be 0

That will of course be set to max out at the users energy_total

My questions are:
Would this be a good way to do this?
Is there a better way to do this?

Also, would this structure work instead and jsut compare everything to last_revoked?

Code: Select all

+----+-------------+--------+--------------+--------+--------------+--------------+
| id | username    | energy | energy_total | health | health_total | last_revoked |
+----+-------------+--------+--------------+--------+--------------+--------------+
|  1 | *********** |     10 |           10 |     10 |           10 |              |
|  2 | *********   |     10 |           10 |     10 |           10 |              |
+----+-------------+--------+--------------+--------+--------------+--------------+
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP MySQL give something in time increments without cron job

Post by onion2k »

The approach you've outlined there would be the best way to do it.
Opalelement
Forum Newbie
Posts: 2
Joined: Sun Aug 10, 2008 4:22 pm

Re: PHP MySQL give something in time increments without cron job

Post by Opalelement »

Thanks
Post Reply