UndeadCircus Posted April 3, 2010 Share Posted April 3, 2010 Hey everyone, I'm new to the forums, but not quite new to PHP...Except it feels like I'm a COMPLETE n00b. I have quite an interesting problem here.. I'll try to explain it in as much depth as I can without it being too confusing. I'm developing an online game and I'm trying to get a progress bar that updates based on what a user's experience points are. I have a table called "levels" setup which is setup so that auto-increment "id" column is the actual level the user can obtain. In this table, I have levels_minfans and levels_maxfans. The rows inserted into this table look something like this: levels_id: 1 levels_minfans: 0 levels_maxfans: 74 levels_id: 2 levels_minfans: 75 levels_maxfans: 149 levels_id: 3 levels_minfans: 150 levels_maxfans: 299 Now, what happens is if the user has anywhere between 0 and 74 fans, he is at level 1. When they hit 75 fans up to 149 fans, they are level 2 and so on. Well, I'm trying to get my static progress bar to display the percentage between these two values. Such as if they are level three, I need it so that the progress bar is at zero when they have 150 fans, and increment up to 100 as they hit 299 fans. So, in short, 150 fans really equals zero percent, and 299 fans equals 100 percent. I think that might be in depth enough to understand the problem. I've been dealing with this crap ALL day today and no matter what combination of math formulas I try, I can't seem to get the progress bar to reset at zero everytime a new level is hit and the player has the minimum number of fans for that level and then top out at 100 when they reach the max fans for that level. Does all of this make sense? If so, I'd appreciate any help I can get from you guys. Thank you so much for reading this - hope I can get an answer soon! Ricky Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/ Share on other sites More sharing options...
Daniel0 Posted April 3, 2010 Share Posted April 3, 2010 [math]progress = \frac{current - min}{max - min}[/math] Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036308 Share on other sites More sharing options...
UndeadCircus Posted April 3, 2010 Author Share Posted April 3, 2010 Man.. I was really hoping that would work because it seemed like such an easy answer.. But still, no such luck. I appreciate your reply though! Maybe you have another idea? Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036312 Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 I have a table called "levels" setup which is setup so that auto-increment "id" column is the actual level the user can obtain. Although this may seem like a smart move it also has some drawbacks, for example: if you want to replace an existing levels name (from a back-end) suddenly those that are level 3 are level-less. Now, what happens is if the user has anywhere between 0 and 74 fans, he is at level 1. When they hit 75 fans up to 149 fans, they are level 2 and so on. Well, I'm trying to get my static progress bar to display the percentage between these two values. Such as if they are level three, I need it so that the progress bar is at zero when they have 150 fans, and increment up to 100 as they hit 299 fans. So, in short, 150 fans really equals zero percent, and 299 fans equals 100 percent. (progress - levels_minfans) / levels_maxfans - levels_minfans * 100 Edit: damn Daniel0 beat me to it. Edit2: I forgot to incalculate the player's progress Man.. I was really hoping that would work because it seemed like such an easy answer.. But still, no such luck. I appreciate your reply though! Maybe you have another idea? Multiply by 100 to get a percentage. Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036315 Share on other sites More sharing options...
Daniel0 Posted April 3, 2010 Share Posted April 3, 2010 Man.. I was really hoping that would work because it seemed like such an easy answer.. But still, no such luck. I appreciate your reply though! Maybe you have another idea? How doesn't it work? Take level 2 for instance: min = 75; max = 149 current = 75 => progress = (75-75)/(149-75) = 0.0 = 0% current = 112 => progress = (112-75)/(149-75) = 0.5 = 50% current = 149 => progress = (149-75)/(149-75) = 1.0 = 100% Multiply by 100 to get a percentage. 0.1 = 10% 0.1 * 100 = 10% * 100 = 1000% Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036317 Share on other sites More sharing options...
UndeadCircus Posted April 3, 2010 Author Share Posted April 3, 2010 Thanks a MILLION to you guys! I actually ended up going with Daniel0's answer, I just had to add an extra "*100" to the end of the equation and it balanced everything out. Holy crap.. All day I've been working on this and it comes down to a very simply formula. Words can't describe how thankful I am to you guys right now, lmao. @ignace: Yea, about the database levels.. I knew it could cause some issues like that down the road, but it was the most practical way I could think of in dealing with user's levels later down the road when it comes to the way the game is handling leveling up, etc. But I appreciate the feedback nonetheless. I'll definitely keep what you said in mind! Thanks again guys.. Major props to you for the help. Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036318 Share on other sites More sharing options...
Daniel0 Posted April 3, 2010 Share Posted April 3, 2010 Okay, so let me explain why it works... Basically you're trying to figure out how far the user is from getting to the next level. For each level you have the minimum required fans. This is acting as an offset, so you essentially just need to subtract this from the other parameters. Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036322 Share on other sites More sharing options...
UndeadCircus Posted April 3, 2010 Author Share Posted April 3, 2010 Yea, after I plopped the code in and tweaked it a little, I started to study it to find out exactly what's going on.. And it makes me feel kind of senseless knowing that it was so easy. But then again, being exausted and completely braindead from staring at code all day will have impacts on your brain that push out even the most remedial of tasks, haha. But again I say - thanks a ton. I can sleep soundly tonight knowing one of the biggest aspects of this game are now complete haha. Quote Link to comment https://forums.phpfreaks.com/topic/197444-what-seems-to-be-an-easy-percentage-problem-actually-seems-impossible/#findComment-1036325 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.