seany123 Posted September 7, 2008 Share Posted September 7, 2008 Is it possible to Increase a Number by a certain percent Using Crons example... Maxenergy = 100. 20% of maxenergy = 20. so cron updates value by 20. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/ Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 what?? CRON is the aility to execute a script at a certain time Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635533 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 okay is it possible to create a Script which will do this? Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635534 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 increment what a field in a database? Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635535 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 In a database? >_> Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635537 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 yes sorry a value in a database.... has to work out 20% of $maxenergy... and give it to $energy. --------------------------------------------------------------------------- to help people understand what i mean... im building a game with php.... i have a energy bar which i need going up 20% every 5 minutes. $maxenergy = 100 (but can change) now i could just : $energyupdate = $player->energy + 5; $query = $db->execute("update `players` set `energy`=? where `id`=? and `energy`<`maxenergy`", array($energyupdate, $player->id)); but because maxenergy can change +5 would be unfair on alot of players... so i want it to gain 20% of maxenergy instead. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635543 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Cron: */5 * * * * /usr/bin/php -f yourscript.php Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635550 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 its a script that im looking for help with. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635832 Share on other sites More sharing options...
BlueSkyIS Posted September 7, 2008 Share Posted September 7, 2008 so you need to know how to increase a value by 20%???? $energyupdate = $player->energy * 1.2; Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635837 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 <?php $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $energy = $row['energy'] + ($row['maxenergy'] * .20); $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy; $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']); mysql_query($query) or die(mysql_error()); } ?> Math skills really come in handy for programming. =/ Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635842 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 So the code you now said will check what the $Maxenergy is... then give 20% of $maxenergy to $energy? Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635850 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Yeah. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635853 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 okay ill test it out and see what happens thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635858 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 At Darkwater that is disgustingly bloated You can write your query to do update all rows Update `players` SET energy = (energy+(maxenergy*.2)) Now if you don't have a way to set a max value to energy (so E is not greater than Emax)you need to add a conditional statement 'but don't do a loop to run 5000 queries run just 1 with a condition part please Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635956 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 You need to make sure it's not greater than the maxenergy. Otherwise I would have done it in one query... Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635958 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 you both got me confused lol i cant even test it untill i get mywebsite working again. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635966 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 You need to make sure it's not greater than the maxenergy. Otherwise I would have done it in one query... Like I said I believe you can set a MAX value for a field to take (and if greater it automatically sets to be what ever MAX is) However you can do conditional like this (untested but can be debugged very easily) UPDATE `players` if((energy+(maxenergy*.2) <= maxenergy), energy = energy+(maxenergy*.2), energy = maxenergy ) Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635967 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Yeah, I know cooldude, but it doesn't really matter for something like this, considering that it'll be run via cron and it'll only take like, 0.3 seconds to run anyway. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635970 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 Yeah, I know cooldude, but it doesn't really matter for something like this, considering that it'll be run via cron and it'll take like, 0.3 seconds to run anyway. When u can run 1 query instead of 1 query for each user (which in this forum would be near 69 thousand) I think you rather run 1 query. 69 thousand queries * 4 times an hour * 24 hours a day = 3.312 million queries a day 1 query * 4 times an hour * 24 hours a day = 96 queries a day I leave you to make your own predictions Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635972 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 MySQL still has to process the data and do multiplications and conditionals (which may be slower in MySQL, I don't use them that often so I don't know) for every single row, so your comparisons are not true at all. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635983 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 Less queries that are written properly are always better. A query that doesn't have a WHERE statement means the table needs not be seeked each time (as you are doing 69k seeks) The simple math being executed is a simple check and takes aggregate time to run (even at running it twice) + because I have no conditional limiter to my query the given table is only seeked (and updated) in a single pass. You can run test but I have a strong feeling I win in this scenerio Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635987 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Yeah, but I think the result will be negligible, and he's using cron anyway, so a user doesn't need to sit there waiting... Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635990 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 I just ran a test, and mine took about 21 seconds to run with 70000 rows. I didn't try yours because it throws a syntax error I think and I don't feel like cleaning it up. 70000 rows is quite a lot of rows though, and if he was using cron I can't see the issue. If you clean up your query I'll try testing it too. Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-636003 Share on other sites More sharing options...
seany123 Posted September 7, 2008 Author Share Posted September 7, 2008 maybe i could get you two intelligent people to look on this problem instead... http://www.phpfreaks.com/forums/index.php/topic,215492.0.html Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-636005 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 well cron still is using processing on your server and if the script is using processing power (or mysql bandwidth) that is bandwidth and processing power that can't be used elsewhere. Sure a user isn't sitting their watching the cron run but you could be bogging down (or crashing your server) because of the cron job Quote Link to comment https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-636010 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.