Jump to content

Change Time And Amount Of Money


kirbyweb
 Share

Recommended Posts

How do I make this code so I get 1 dollar every 5 seconds?here is the code.

<?phpsession_start();$user = $_SESSION['username'];//open my database$connect = mysql_connect('_______', '___', '____') or die('Couldn\'t connect!');mysql_select_db('__________');$query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'");$username = mysql_fetch_array($query);echo 'Logged user has: '.$username['money'].' money';//money amount to add for each round$add_money = 1;//time now, and round time in seconds (1h=60*60seconds=3600)$time_now = time();$round = 3600;//time of last update+time needed must be smaller then time now to updateif (($username['lastupdate']+$round) <= $time_now) {//see how many rounds (hours) were there from last update$nr_rounds = floor(($time_now-$username['lastupdate'])/$round);//calculate how much money user gained$all_money = $nr_rounds * $add_money;//calculate how many rounds in seconds (how many hours in seconds)$add_time = $nr_rounds * $round;//lets update users tablemysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+'$add_time', money=money+'$all_money' WHERE id = '$username[id]'") or die(mysql_error());//here you can refresh page (so you can see progress) or select user again using login}?>

Edited by kirbyweb
Link to comment
Share on other sites

This is where it sets the amount of money per unit time:

$add_money = 1;

And this is where it calculates the number of units of time and multiplies:

$nr_rounds = floor(($time_now-$username['lastupdate'])/$round);//calculate how much money user gained$all_money = $nr_rounds * $add_money;

Previously it had set $round to the number of seconds in an hour.

Link to comment
Share on other sites

probably somewhere in your time forumla. If you're making too much money, do what JSG said.

Well, if it already adds 1 dollar every certain amount of seconds then you have 2 options, you can either change the amount it adds per second or you can change the formula where it multiples the amount by the time.
You can always adjust the values through trial and error and see how that works out. Edited by thescientist
Link to comment
Share on other sites

OK, forget this question.My new question is how can I make a number add one every 5 seconds without being able to refresh the page, like it counts on the page.So it counts up 1 number every 5 seconds without the page having to refresh, I cannot find any tutorials so if someone could wright this code I would learn how, thanks.

Link to comment
Share on other sites

If you want a continuous counter on a page you need to use Javascript. Javascript has functions called setTimeout and setInterval that let you run another function after a certain amount of time, so you would have a function that increases the counter and then use setTimeout or setInterval to schedule it to run after a certain amount of time. That's not going to update the database, but that's how you get a real-time counter on your page.

Link to comment
Share on other sites

You're talking about several different things. One is the script you already have, where it figures out the last time the user was online and calculates the amount they should get, and updates the database. The other is a Javascript counter that just counts up on the page and doesn't update the database. Both are possible. You can use PHP to output the current value so that the Javascript counter knows where to start at.

Link to comment
Share on other sites

That's exactly what the script you posted in post #1 is already doing. It sets how much money to give per unit time, then calculates how many units of time there were, then multiplies. It's just math. That script uses the time function, which returns the Unix timestamp. In PHP, the Unix timestamp is the number of seconds since 1/1/70, so it's just a number. If the timestamps are all you use, then you can store the last time the user visited in the database as a timestamp. When the user loads a page, you check the last time in the database and get the current timestamp using the time function like your code shows, then subtract the difference to get the number of seconds since the last time they loaded a page. Then update the database with the current timestamp and the new money. I prefer to use timestamps in my own programs, they make things like this easier.This information isn't secret, if you do a Google search for "php time" or "php timestamp" you'll find a ridiculous amount of information about it. If you're going to be successful at programming you need to learn how to do research. I'm not trying to be hard on you, but you ask a lot of questions that you need to be able to find the answers to on your own. I look things up on php.net and on Google every single time I program, the reason I don't ask questions on this forum is not because I never get stuck.

Link to comment
Share on other sites

In that script $round is 3600, the number of seconds in one hour. $nr_rounds is the number of rounds, so it's the number of hours since the last visit. It multiplies the money by the number of rounds, so it's calculating the money per hour, not per minute. In the script, $username['lastupdate'] is the timestamp for the last visit. Just read the comments in the code, they explain exactly what each line is doing.

Link to comment
Share on other sites

This still does not answer my question about how to make it so I do not need to refresh the page, so like it counts up.And also to make it add one dollar every minute, I would change the $round to 60 right?Well when I do that it goes up to like 30kAlso how do I make it so when I click refresh it does not update the money until 1 minute.

Edited by kirbyweb
Link to comment
Share on other sites

I don't give out my email address for support, I'm trying to show you how to solve these problems, not do it for you.

This still does not answer my question about how to make it so I do not need to refresh the page, so like it counts up.
Once again, that's the Javascript portion, not PHP. Are you bothering to click on anything I'm giving you, or are you just waiting for me to write the code for you? You use setTimeout or setInterval to create a Javascript counter.
And also to make it add one dollar every minute, I would change the $round to 60 right?
$round is the number of seconds to add the money for, so if you want to add money every minute then yeah, you change it to 60.
Well when I do that it goes up to like 30k
That doesn't really tell me anything, where did it start at? What did the timestamp in the database start at? Let me guess, you started the timestamp at 0, right? Did you read what I wrote about what the timestamp means?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...