cdoyle Posted March 17, 2008 Share Posted March 17, 2008 Hi, I have a update query, that is run with a cron every 15 minutes. $query = $db->execute("update `players` set hp = hp + 10" ); What it does is increases a players hp 10 every 15 minutes. this part is working good, but here is the problem. Depending on the level a player is, they have a maxhp (there is a field name in the players table called maxhp) How can I make it so it updates up to the maxhp and then stop? Right now, it just keeps going and going and going (lol) Quote Link to comment Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 you could query the players first. I don't know your query functions so will use basic php <?php $query = "SELECT `hp, `maxhp` FROM `players`"; $result = mysql_query($query) or die(mysql_error()); while($r = mysql_fetch_assoc($result)){ if($r['hp'] < $r['maxhp']){ mysql_query("UPDATE `players` SET `hp` = `hp` + 10"); } } Ray Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 17, 2008 Share Posted March 17, 2008 I'd use this one liner with an IF statement: $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" ); Quote Link to comment Share on other sites More sharing options...
cdoyle Posted March 17, 2008 Author Share Posted March 17, 2008 I'd use this one liner with an IF statement: $query = $db->execute("update `players` set hp = IF((hp + 10)>maxhp, maxhp, (hp + 10))" ); That worked great!! Thank You Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.