Nov 24, 2009
Pages: 1, 2, 3, 4, 5, 6

How To Make A Value In The Database Raise Every Minute.

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

How To Make A Value In The Database Raise Every Minute.

Feelay
still showing 1 hp =/

Comment/Reply (w/o sign-up)

pyost
Let's echo the variables once more...

Comment/Reply (w/o sign-up)

Feelay
1202504970
0
275
0
1202504970

this is what i get

Comment/Reply (w/o sign-up)

pyost
Which number is what variable?

Comment/Reply (w/o sign-up)

Feelay
Resource id #16 $updatedR
1202505413 $updated
0 $difference
Resource id #17 $healthR
1 $health
0 $addHealth
1202505413 $updated

Comment/Reply (w/o sign-up)

pyost
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 wink.gif

Comment/Reply (w/o sign-up)

vujsa
Well, let's try and get this figured out now!

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:
  • Load the system and get the current time.
  • Check the database and get the last update time (global)
  • Calculate the difference between the current time and the last update time.
    $time_difference = $current_time - $last_update_time
  • We then convert the number of seconds that have passed to the number of decimal hours that have passed
    $hours_passed = $time_difference / 60 / 60
  • We then calculate the number of whole hours that have passed.
    $full_hours_passed = floor($hours_passed)
    Round down to the nearest whole hour
  • Determine the number of seconds left over.
    $remaining_seconds = $time_difference % (60 * 60)
  • We have to add the remaining seconds to the new update time if there is an update.
    if($full_hours_passed > 0) {$new_update_time = $current_time + $remaining_seconds; }
  • We then calculate the number of HP to increase by based on the number of hours passed and the rate of increase
    $hp_increase = $full_hours_passed * 5
  • Finally, we increase each user's HP (Requires MySQL 5.0+)
    This is kind of tricky because we have to be sure that we don't go over 100 if that is the highest possible HP value
    mysql_query("UPDATE user SET hp = CASE WHEN hp + $hp_increase <= 100 THEN hp + $hp_increase ELSE 100 WHERE hp < 100")
Now, this will update every user at one time and won't place too high of a load on the server. Don't forget to update the last update time in the database! Don't forget that we only update the database if at least one full hour has passed.

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. cool.gif

vujsa

 

 

 


Comment/Reply (w/o sign-up)

Feelay
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..

Comment/Reply (w/o sign-up)

vujsa
QUOTE(Feelay @ Feb 9 2008, 04:37 PM) *
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

Comment/Reply (w/o sign-up)

Feelay
hmm =/ Vujsa. Nothing happened =S I've bee waiting for 12 minutes, but nothing changed.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2, 3, 4, 5, 6
Similar Topics

Keywords : make, database, raise, minute,

  1. Five Common Php Database Problems
    (0)
  2. How To Show Serial Nums In PHP Table For Contents Of MySQL DB
    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
    outputted contents of mysql database. I used a table created in PHP to output content (i.e. an
    alumni database) and I created a column for S/N, so that at a glance anyone can tell how many
    members have registered. Thanks house. Neyoo....
  3. Need Help With 2-Way Password Encryption
    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
    in a database, but MD5 and SHA1 are hashing functions; they only go one way. So then how do I let
    users know what their password is if they forget it? I suppose I need a two-way encryption method,
    right? Can somebody please tell me what the easiest way to solve my problem is with PHP and MySQL?
    Thanks, Trevor....
  4. What Database Do You Use With PHP
    Regarding PHP supported database format (5)
    There are different database backends supported by PHP. However, most of us probably use MySQL and
    the books on PHP mostly use MySQL as the backend database. These are the currently supported
    database format: 1. dBase 2. FrontBase (functional since DB 1.7.0) 3. InterBase (functional since
    DB 1.7.0) 4. Informix 5. Mini SQL (functional since DB 1.7.0)6. Microsoft SQL Server (NOT for
    Sybase. Compile PHP --with-mssql) 6. MySQL (for MySQL 7. MySQL (for MySQL >= 4.1) (requires PHP 5)
    (since DB 1.6.3) 8.Oracle 7/8/9 9. ODBC (Open Database Connectivity) 10. PostgreSQL 11. SQL....
  5. [php] Index.php?section=xx&pag=yy
    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
    SECTIONS and PAGES. Benefits: -You have to create just the text of your pages, no create ech page
    with the entire layout again. -If its just the text that is included, you just have to have one page
    with the layout, witch is the INDEX.PHP. -If you chanche the layout in the index.php, you DONT HAVE
    TO change in the other pages. Here is the code: CODE
    //-----------------------------------------// //ACAF Paginação                           //
    //by Alexandre Cisneiros    ....
  6. Need Help With A PHP - MySQL Registration Script
    Wont INSERT into the database (13)
    hey well can some one helpme make this code work it won't INSERT INTO THE DATABSE CODE #
    register1.php # common include file to MySQL include("DB.PHP"); $Username=$_POST ; $Password=$_POST
    ; $Name=$_POST ; $Last=$_POST ; $Sex=$_POST ; $Month=$_POST ; $Day=$_POST ; $Year=$_POST ;
    $Adresse=$_POST ; $City=$_POST ; $State=$_POST ; $Zipcode=$_POST ; $Country=$_POST ; $Phone=$_POST ;
    $Email=$_POST ; $Father_Name=$_POST ; $Mother_Name=$_POST ; $Parent_Phone=$_POST ;
    $Parent_Email=$_POST ; $Level=$_POST ; $Academic=$_POST ; $Image_Link=$_POST ; $sql9="INSERT INTO
    U....
  7. How Do I Connect Php To A Database ?
    (3)
    How I can to connet to DB in PHP?....

    1. Looking for make, database, raise, minute,

See Also,

*SIMILAR VIDEOS*
Searching Video's for make, database, raise, minute,
advertisement



How To Make A Value In The Database Raise Every Minute.

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com