kirbyweb Posted November 12, 2009 Share Posted November 12, 2009 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}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 It's already calculating the time since the last update and assigning money, correct? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Yes but how do I make it so it adds 1 dollar every 5 seconds. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Well I never really used time function and when I tried changing round to 1 it still did the same thing where is it multiplying? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Ok, so still how do I make it so I get 1 dollar per 5 seconds. Because right now its getting like 10k per second. Link to comment Share on other sites More sharing options...
thescientist Posted November 13, 2009 Share Posted November 13, 2009 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. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 I tried but I could not figure out how. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 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 More sharing options...
thescientist Posted November 13, 2009 Share Posted November 13, 2009 What did you try changing? What about it didn't work? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Well I just want to figure out how to make it count up 1 dollar every 5 seconds can anyone help, like I said I do not want to have to refresh the page I just want it to count up on the page. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 could I use it for the money?Or do I need a php code for that? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Like I need the money to go to 500 and it gets 1 dollar ever 30 seconds, and it updates it when he is offline also.And it would count up on the page without it being refreshed. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 How though? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 http://lmgtfy.com/?q=setinterval Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 Is there a tutorial on php time or something.All I am asking is how do you make the number count every so and so seconds or minutes, in php, that is all I need to know please help. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 OK, the only thing is I cannot figure out the time, the $round part how do I figure out which number is one minute.And this code I need to refresh the page to see, if I wait 2 minutes and refresh the page, it will add the same amount as one second, do you know why. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 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. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 13, 2009 Author Share Posted November 13, 2009 If you want email me kirbyfrogfan@gmail.com with a gmail account so we can use chat, it will be so much easier. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 13, 2009 Share Posted November 13, 2009 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 30kThat 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.