| | still showing 1 hp =/ |
| Nov 24, 2009 |
|
Let's echo the variables once more...
Which number is what variable?
Resource id #16 $updatedR
1202505413 $updated 0 $difference Resource id #17 $healthR 1 $health 0 $addHealth 1202505413 $updated
I am getting really confused... Sorry, but I can't help you anymore tonight, as I have some things to do. I'll see to this problem tomorrow
I think a simpler approach is in order. Instead of looking at each user individually, lets look at the whole system! I'll start by explaining what I have in mind:
Here is what I had in mind: CODE $current_time = time(); // get the current server time in second $last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0); // This could be anywhere even in a separate text file! $time_difference = $current_time - $last_update_time; $hours_passed = $time_difference / 60 / 60; // convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour $full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour $remaining_seconds = $time_difference % (60 * 60); // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0 if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * 5; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time"); } The reason that we have to add the remaining seconds back to the current time is basically because we didn't use them and as a result, if we left them out then you would probably update more than 24 times each day! Now, we could modify this script to update by 1 every 20 minutes if needed. One last important note: I don't know how you intend to connect to and use your database. Adjust your PHP to match whatever technique you will use to read information from the database results. I hope this helps. vujsa
I hope it does :S I have to wait 1 hour. is there any way to make the time shorter (every 1 min or something). when i tryed, the hp value showed 1 all the time, so I changed it back.
btw, i changed the 100 in the mysql_query to maxhp, because i have a table named that, and now all users have 280 hp =/ how can i solve that =/ =? the users maxhp depends on the users level, but all users have the same hp as the highest level.. I hope it does :S I have to wait 1 hour. is there any way to make the time shorter (every 1 min or something). when i tryed, the hp value showed 1 all the time, so I changed it back. btw, i changed the 100 in the mysql_query to maxhp, because i have a table named that, and now all users have 280 hp =/ how can i solve that =/ =? the users maxhp depends on the users level, but all users have the same hp as the highest level.. Is that a table or a field named maxhp? If it is a table, then we have some additional work to do on the system. Otherwise, you should be okay. Now, for shortening the time, consider the following: 7200 / 60 / 60 = 2 Likewise: 7200 / ( 60 *60 ) = 2 Now, we know that 3600 second equals 1 hour so lets create a variable for that! CODE $frequency = 3600; Might as well make a variable for the number of HP to increase by each interval: CODE $increment = 5; Now, we modify the previous code to include that! CODE $frequency = 3600; $increment = 5; $current_time = time(); // get the current server time in second $last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0); // This could be anywhere even in a separate text file! $time_difference = $current_time - $last_update_time; $hours_passed = $time_difference / $frequency; // convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour $full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour $remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0 if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time"); } Note that I replaced / (60 * 60) and / 60 / 60 with / $frequency. Additionally, the value of 5 for the increment value was change to the variable $increment. These changes make it easier to change how often and how much the HP in increased for the users. See, this way we could do the following to increase the value by 1 point every 12 minutes instead on 5 points every 60 minutes: CODE $frequency = 3600 / 5; // 720 seconds = 12 minutes $increment = 5 / 5; // 1 Or even better, we could add one more variable as the multiplier! 7200 / ( 60 *60 ) = 2 Now, we know that 3600 second equals 1 hour so lets create a variable for that! CODE $frequency = 3600; Might as well make a variable for the number of HP to increase by each interval: CODE $increment = 5; Now, we modify the previous code to include that! CODE $frequency = 3600; $increment = 5; $current_time = time(); // get the current server time in second $last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0); // This could be anywhere even in a separate text file! $time_difference = $current_time - $last_update_time; $hours_passed = $time_difference / $frequency; // convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour $full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour $remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0 if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time"); } Note that I replaced / (60 * 60) and / 60 / 60 with / $frequency. Additionally, the value of 5 for the increment value was change to the variable $increment. These changes make it easier to change how often and how much the HP in increased for the users. See, this way we could do the following to increase the value by 1 point every 12 minutes instead on 5 points every 60 minutes: CODE $multiplier = 1 / 5; // One fifth of an hour is 12 minutes $frequency = 3600 * $multiplier; // 720 seconds = 12 minutes $increment = 5 * $multiplier; // 1 So the final code would look like this: CODE $multiplier = 1 / 5; $frequency = 3600 * $multiplier; $increment = 5 * $multiplier; $current_time = time(); // get the current server time in second $last_update_time = mysql_result(mysql_query("SELECT update_time FROM update_table"), 0); // This could be anywhere even in a separate text file! $time_difference = $current_time - $last_update_time; $hours_passed = $time_difference / $frequency; // convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour $full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour $remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0 if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100"); mysql_query("UPDATE update_table SET update_time = $new_update_time"); } So, now you only need to modify the first 3 variables to change the frequency and amount of HP that is increased. However, if you have a field in the 'user' table named maxhp for EACH player, then you should change every instance of the number 100 in the query to maxhp like this: CODE mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= maxhp THEN hp + $hp_increase ELSE maxhp WHERE hp < maxhp"); Now as I hinted previously, if you are using some other method for storing each users maximum hp value, then I'll need some database information from you to know how to write the correct MySQL query for this operation. I must warn against trying to update the system every 1 minute as this will overtax the server and probably cause all of us a lot of trouble with our hosting accounts. It would be okay for testing purposes but in a live environment, it will probably crash the server. I hope this gets you on your way to finishing your project. vujsa
hmm =/ Vujsa. Nothing happened =S I've bee waiting for 12 minutes, but nothing changed.
Similar Topics
Keywords : make, database, raise, minute,
Serial Numbering for output contents of mysql in php table (4) Hello there, I'm looking for some education. How would you show the serial numbering for How to properly store passwords in a database (8) Every article I've read on the internet so far suggests using MD5 or SHA1 to "encrypt" passwords Regarding PHP supported database format (5) There are different database backends supported by PHP. However, most of us probably use MySQL and No MySQL or any other database (6) Hi everybody. This is my 3rd script, but this dont use MySQL It does this: divide the site in Wont INSERT into the database (13) hey well can some one helpme make this code work it won't INSERT INTO THE DATABSE CODE # (3) How I can to connet to DB in PHP?.... Looking for make, database, raise, minute,
|
![]() How To Make A Value In The Database Raise Every Minute. |
Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com