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

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

free web hosting

Read Latest Entries..: (Post #50) by Feelay on Feb 17 2008, 10:06 PM.
I understand how theese stuff works now (the time() function and stuff).But is it possible to make it update without a user being online =? Or must the script be loaded by a user, to run?
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

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

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

Feelay
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

Comment/Reply (w/o sign-up)

Eggie
you can either raise 0.08 hp per minute or you can raise 5 HP per hor which is better if you ask me...
CODE
<?php
$cpass=insert_your_own_password_here;
$revive=$_GET["revive"];
if ($revive == $cpass) {
    mysql_query("update players set hp=hp+5");
    print "Revive Complete";
    exit;
}
?>

now go to cron jobs in cpanel...let's say that the above code is in your www/ folder named revive.php
write
CODE
lynx -dump http://yourgameurl.com/folder/revive.php?revive=insert_your_own_password_here > /dev/null

and set it to hourly(to do that script every hour)
did that help?i hope it did!
Eggie

Comment/Reply (w/o sign-up)

Feelay
It helped a bit, Thanks. But, how do I do it without the cron jobs =?

Comment/Reply (w/o sign-up)

pyost
Don't update it all the time, only when the value is required. You would just need to remember the last time the value is updated, calculate how many minutes have passed, and then set the new value accordingly. Not only will you put less load on the server, but you will also avoid using cron jobs smile.gif

Comment/Reply (w/o sign-up)

Feelay
But how do I do that =/
Thats the thing I don't know =(

Comment/Reply (w/o sign-up)

pyost
First of all, you need to take into consideration only those scripts that display/access a player's health. When such script is ran, it should retrieve the data from the database, check whether the player's health should be updated, and act accordingly. This is how I would do it.

Let's suppose you have already retrieved $health and $updated. The first variable contain the current health, and the second when the health was last updated (UNIX timestamp).

CODE
$currentTime = time(); // we need this in order to calculate how much time has passed

$difference = $currentTime - $updated;

$addHealth = floor($difference / 3600) // 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 > 100) { $health = 100; } // we don't want the health to go over 100!
$updated = time(); // we also need to update the alteration time.


Now you would just need to enter the new value of $health and $updated into the database, without the need to read them again in this script.

I have not tested this code, so it might not work properly.

 

 

 


Comment/Reply (w/o sign-up)

Eggie
why don't you want to work with cron jobs...it's not that hard...you have the script i wrote for you and you just need to adjust it and it will be over...
i always like helping people with something i know and that's what i ask from other people...to help me if i have problems

Comment/Reply (w/o sign-up)

pyost
Cron jobs can be useful at times, but here they would be too much. You would have a script that runs every sixty seconds and updates all the players. Sure, for only several players it is OK, but just imagine updating several thousands of players - it would definitely put immense load on the server (unnecessary load, that is).

Comment/Reply (w/o sign-up)

Eggie
oh...i didn't know it's that much....but i only learned to do it with cron jobs so i posted that script before...i'm a newbie in scripting so i really don't know about that...(i'm currently getting my register script to work and helping bhupinunder with his game)
@pyost:you have msn???if you do pls send me PM with it..i need help asap

Comment/Reply (w/o sign-up)

Feelay
kill me if you want.. but I still don't understand xD
You mean that the "$Health" is the mysql_query for the current health and that the "$updated" is for the "last time the health was updated" colum = Both is mysql_queries =?

Comment/Reply (w/o sign-up)

Latest Entries

Feelay
I understand how theese stuff works now wink.gif (the time() function and stuff).

But is it possible to make it update without a user being online =? Or must the script be loaded by a user, to run?

Comment/Reply (w/o sign-up)

sparkx
*Sigh* I thought you would be able to add in bits of code here and there to make it custom but apparently you A: Didn't read my post's content or B: Simply didn't understand it.
I (being a lazy programmer) forgot Step 6 in my example but I listed it above in the content but just not in the example. Anyway after $CH=...; you should add if($CH>280){$CH=280;} now if you have subtract simply add (after the same check) if($CH<0){$CH=0;} This will limit the $CH var to either 280 or 0 you could use both but I that is only if your mysql database stored a negative variable before and when you added the time the variable was still negative (or the value before was greater then 280 if you are subtracting). Hopefully you checked that on your attack script otherwise you will have some problems.

Thanks,
Sparkx

Comment/Reply (w/o sign-up)

Feelay
Don't think that worked xD
I have 601600163 / 280 HP xD

Comment/Reply (w/o sign-up)

sparkx
You just want to update a value every minute you may need to just check with the database (as vujsa said before). This is quite simple to achieve. I use this method often here is a basic blueprint of what is going to occur:
-Step 1: only call when asked (so if selected for attack script or profile). I suggest that you make it into A: a function or B: just make it a require friendly page. Easy in easy out est. est.
-Such as you put in the $username=$name; --> Require --> $Current_health=$currenthealth.
-Step 2: Select for database where username=$username and extract two entries HP and TimeUD (time Updated).
-Step 3: Time() - TimeUD = TimeB (Time between).
-Step 4: TimeB*Rate (should be a number you could however put in an equation for health)=HPA (HP Added).
-Step 5: HP+HPA=NHP(New HP).
-Step 6: Simple if If(NHP>100){NHP=100;}
-Step 7: Update Database with Time() (from above). Update Database with NHP.
-Step 8: Finally just set $currenthealth and finish.

With that said you 'should' be able to figure it out. Basically we are NOT updating the database each minute but rather each time we need to. Then we are simply getting the value. Just change a few things to decrease health over time (stabbed and loosing blood, slowly dieing over time est.).

Here is my Untested Example (Change it accordingly):
CODE
<?php
//In: $username
//Out: $CH
//Only use once each load
//Require with require(); See Manual.

$row = mysql_fetch_array(mysql_query("SELECT * FROM replace_w_db WHERE UserN='$username'") or die(mysql_error()));
$currentHP=$row['HP'];
$LUT=$row['LastUpdatedTime'];
$currentTime=Time();
$TD=$currentTime-$LUT;
$HPD=$TD*0.1;//0.1 HP gain per secound.
$CH=$currentHP+$HPD;//Add HP (change to - if subtract).

//Update DB
$result = mysql_query("UPDATE replace_w_db SET HP='$CH' WHERE UserN='$username'")
or die(mysql_error());  
$result = mysql_query("UPDATE replace_w_db SET LastUpdatedTime='$currentTime' WHERE UserN='$username'")
or die(mysql_error());  

//Finish off.
?>



It should work (hopefully). If not, you know how to PM.
Good Luck,
Sparkx


EDIT: Spelling + Grammar Error fixed.
Oh ya and if you want every minute just divide $TD by 60 ex: $TD=$TD/60; (insert just after:$TD=...wink.gif

Comment/Reply (w/o sign-up)

Feelay
vujsa. the value is still not updating ?

the $full_hours_passed sometimes reaches 0, and then the $hp_increase and the $new_update_time reaches nothing (not even 0).
look:

CODE
1203101853 current_time
1203101821 last_update_time
32 time_difference
0.533333333333 hours_passed
0 full_hours_passed
32 remaining_seconds
new_update_time
hp_increase
60 frequency
10 increment


And this:

CODE
1203105671 current_time
1203105541 last_update_time
130 time_difference
2.16666666667 hours_passed
2 full_hours_passed
10 remaining_seconds
1203105661 new_update_time
20 hp_increase
60 frequency
10 increment


but this is a lot later..
2 full_hours_passed = 20 hp_increase.

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