feelay Posted February 6, 2008 Share Posted February 6, 2008 Hi. I am trying to figure out how to make a value in the database raise every minute. Lets say, I want the HP to raise every minute. The max HP is 100. I want it to raise 5 HP/ Hour. And the player don't have to be online. Anyone who know how I can do it =? Thanks for any help. //Feelay Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/ Share on other sites More sharing options...
PC Nerd Posted February 6, 2008 Share Posted February 6, 2008 hi.. soudns liek your writing a game - meanign youll ve getting a lot of database trafic. instead why not have a text file for the HP - and then hevery tiem someone reads it - check if the time is + 60 seconds - then write new values to it. eg have the file say: last_time: "timestamp" HP: 5; then jsut check to see if the timestamp is as above and then do the same. you could do the same through mySQL if you wanted. gdlk Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459569 Share on other sites More sharing options...
feelay Posted February 6, 2008 Author Share Posted February 6, 2008 I don't know how to user the timestamp function =/ Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459605 Share on other sites More sharing options...
Aureole Posted February 6, 2008 Share Posted February 6, 2008 <?php $current_timestamp = time(); $current_date = date( 'l jS M Y, g:i A', $current_timestamp ); echo( 'The current time-stamp is: ' . $current_timestamp . '. To put that into a human-readable format, the current date from afore-mentioned time-stamp is: ' . $current_date ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459609 Share on other sites More sharing options...
Sulman Posted February 6, 2008 Share Posted February 6, 2008 If your on a *nix system you could consider a cron job: http://www.astahost.com/info.php/cron-jobs-tutorial_t2324.html Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459613 Share on other sites More sharing options...
feelay Posted February 6, 2008 Author Share Posted February 6, 2008 Maybe you think I am an Idiot now.. But I still don't understand how i can make the value in the Database change, when using the timestamp function =/ @sulman, I ain't using unix. Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459752 Share on other sites More sharing options...
Aureole Posted February 6, 2008 Share Posted February 6, 2008 If you want it so when the database is updated, the time-stamp changes to the current time-stamp then just use MYSQL's NOW() function. Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-459781 Share on other sites More sharing options...
feelay Posted February 8, 2008 Author Share Posted February 8, 2008 ok.. some people on another forum helped me a bit.. but neither me or they can solve this problem.. The health keeps showing 1HP when the max is 280, and the hp should raise 5hp every 2 seconds.. Here is the code: <?php session_start(); include "database.php"; include "hptime.php"; include "level.php"; $nuser=mysql_real_escape_string($_SESSION['user']); $auser=mysql_real_escape_string($_SESSION['admin']); if($nuser){ $userfinal=$nuser; }elseif($auser){ $userfinal=$auser; } ################################################################# // TID $updatedR = mysql_query("SELECT hptime FROM characters WHERE user='$userfinal'"); //hämta förra tiden från databasen $updated = mysql_result($updatedR, 0, 'hptime'); $currentTime =time(); //tiden just nu $difference = $currentTime - $updated; // HP $healthR = mysql_query("SELECT temphealth FROM characters WHERE user='$userfinal'");//hp, etc. kan du nog fixa från annat värde som du hämtat. $health = mysql_result($healthR, 0, 'temphealth'); // HEAL $addHealth = floor($difference / 2); // we need to know how many hours have passed - we get an round value (though it is a "float" number) by using floor() $addHealth = (int) $addHealth * 5; // this is how much health will be added, if you add 5HP per hour - I'm not sure whether (int) is necessary, but better safe than sory $health += $addHealth; if ($health > $user['maxhp']) { $health = $user['maxhp']; } // we don't want the health to go over max hp! $updated = time(); // we also need to update the alteration time. mysql_query("UPDATE characters SET temphealth='$health' AND hptime='$updated' WHERE user='$userfinal'"); //Uppdatera HP och TIME ?> Thanks for any replie //Feelay Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-462062 Share on other sites More sharing options...
Sulman Posted February 8, 2008 Share Posted February 8, 2008 If you need a value in a database to increase every 2 seconds without any human involvement then your only way is is an automatic script that constantly runs on the server. You could try setting a cookie that stores what the users hp is and what the time was and then when the user logs back in you check that cookie then update the database with the required hp depending on how long they have been logged off. Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-462072 Share on other sites More sharing options...
feelay Posted February 8, 2008 Author Share Posted February 8, 2008 to complicated for my knowledge =/ Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-462077 Share on other sites More sharing options...
Sulman Posted February 8, 2008 Share Posted February 8, 2008 Well then you may be stumped. It mught be a good idea to start increasing your knowledge by reading up on some of the suggestions. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/89682-how-to-make-a-value-in-the-database-raise-every-minute/#findComment-462172 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.