3raser Posted November 28, 2010 Share Posted November 28, 2010 Website: http://novacms.vacau.com/index.php I'm trying to make a calculator for this game I play, it's fully developed, but with one bug. It doesn't do the math right. $goal = The amount of EXP (Experience Points) someone wants to get $exp = The amount of EXP they currently have $npcs_exp["exp"] = The amount of EXP the monster has But, when they enter in the information, the calculation is all screwed up. When I do it, it seems to give me the same number EACH time. The part where the calculation "goes down". Remember, I'm trying to see how many times you need to kill a NPC to achieve your goal. //math $npc_exp_extract = mysql_query("SELECT exp FROM npcs WHERE name='$npc'"); $npcs_exp = mysql_fetch_assoc($npc_exp_extract); $amount = $goal / $npcs_exp["exp"]; Full code: <?php $exp = $_POST["exp"]; $npc = $_POST["npc"]; $goal = $_POST["goal"]; mysql_connect("removed", "removed", "removed"); mysql_select_db("a2248602_npcs"); if(!$goal || !$npc || !$exp) { echo "<center><form action='index.php' method='POST'>GOAL: <input type='text' name='goal'> Current XP: <input type='text' name='exp'>"; echo "NPC: <select name='npc'>"; $npc_extract = mysql_query("SELECT * FROM npcs"); while($npcs = mysql_fetch_assoc($npc_extract)) { echo "<option name='". $npcs['name'] ."'>". $npcs['name'] ."</option>"; } echo "</select> <input type='submit'></form></center>"; } else { //math $npc_exp_extract = mysql_query("SELECT exp FROM npcs WHERE name='$npc'"); $npcs_exp = mysql_fetch_assoc($npc_exp_extract); $amount = $goal / $npcs_exp["exp"]; if($amount=="1") { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more time. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; } else { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more times. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; } } echo "<center>Made by Zaros from <a href='creation-x.net'>Creation-X</a></center>"; ?> Mod edit: DB credentials removed. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/ Share on other sites More sharing options...
BlueSkyIS Posted November 28, 2010 Share Posted November 28, 2010 echo out values and see if they are what you expect. the only other alternative is for us to go through your code, follow the logic and do the math. if you echo the values, php does that for you. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140650 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Unless I'm missing something, it looks like you execute the same query against the database each time without updating/incrementing/decrementing any of the variables or database values, so it's no surprise that the result is the same. Also, off topic, but I noticed one thing that could be streamlined a bit. $amount = $goal / $npcs_exp["exp"]; if($amount=="1") { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more time. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; } else { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more times. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/> } Can be rewritten as $amount = $goal / $npcs_exp["exp"]; echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more time"; echo $amount == 1 ? '.' : 's.'; echo " You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140652 Share on other sites More sharing options...
3raser Posted November 28, 2010 Author Share Posted November 28, 2010 Unless I'm missing something, it looks like you execute the same query against the database each time without updating/incrementing/decrementing any of the variables or database values, so it's no surprise that the result is the same. Also, off topic, but I noticed one thing that could be streamlined a bit. $amount = $goal / $npcs_exp["exp"]; if($amount=="1") { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more time. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; } else { echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more times. You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/> } Can be rewritten as $amount = $goal / $npcs_exp["exp"]; echo "You will need to kill a <b/>". $npc ."</b> <b>". $amount ."</b> more time"; echo $amount == 1 ? '.' : 's.'; echo " You earn ". $npcs_exp["exp"] ." XP for each kill from this NPC. | <a href='index.php'>Do another</a><br/><br/>"; But there is no reason to do that, I believe. People just go to the calculator, type in the information, and it's suppose to do the math. They don't actually submit information into the database. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140655 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 OK, I did miss something then, LOL. I'll have another look at it. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140660 Share on other sites More sharing options...
ignace Posted November 28, 2010 Share Posted November 28, 2010 OK, I did miss something then, LOL. I'll have another look at it. No you are correct! You will need to kill a Black Demon 0.0149175240895 more times. You earn 306150 XP for each kill from this NPC. The number 306150 remains constant because you don't subtract anything from it. And how do you kill a Black Demon 0.0149175240895 or 0.403253307202 times? Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140664 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 In that case, if the numbers are really that close, the ceil() function would be helpful when echoing how many more kills are needed. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140665 Share on other sites More sharing options...
3raser Posted November 28, 2010 Author Share Posted November 28, 2010 OK, I did miss something then, LOL. I'll have another look at it. No you are correct! You will need to kill a Black Demon 0.0149175240895 more times. You earn 306150 XP for each kill from this NPC. The number 306150 remains constant because you don't subtract anything from it. And how do you kill a Black Demon 0.0149175240895 or 0.403253307202 times? You don't need to subtract anything from it. 306150, the amount of the Black Demon, needs to divide into their goal to see how manys time they need to kill it. When it does that, it gives me a number, but even if I change the amount of the goal, it stays the same. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140668 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2010 Share Posted November 28, 2010 Changing the GOAL field changes the result. Changing the XP field has no effect because the value of $_POST['exp'] isn't used anywhere in the calculation Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140674 Share on other sites More sharing options...
3raser Posted November 28, 2010 Author Share Posted November 28, 2010 Changing the GOAL field changes the result. Changing the XP field has no effect because the value of $_POST['exp'] isn't used anywhere in the calculation Thank you! Fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140689 Share on other sites More sharing options...
ignace Posted November 29, 2010 Share Posted November 29, 2010 And how do you kill a Black Demon 0.0149175240895 or 0.403253307202 times? Did you also fix the above? Quote Link to comment https://forums.phpfreaks.com/topic/220073-am-i-just-bad-at-math-or-is-my-calculator-wrong/#findComment-1140844 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.