Jump to content

Am I just bad at math, or is my calculator wrong?


Recommended Posts

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.

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/>";

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.

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.