I'm a n00b when it comes to PhP and I'm having trouble making a calculator for a game I play. The result I keep getting is wrong, to mirror my results the following will need to be inputted into the infrajump.php file:
Current infra level: 10999.99
Desired infra level: 14999.99
5 factories
check interstate system
check Lumber, Iron, Aluminum, Marble, Construction
check Monarchy, Capitalist, Dictatorship, Federal, Republic or Revolutionary
The result is:
Your current infra level is: 10999.99
Your desired infra level is: 14999.99
To reach 14999.99 you will need to buy 4000 infra, which will cost you 230.
The cost is way beyond wrong it should be in the millions if not billions.
Here is the code....
File name: infrajump.php
<html>
<body>
<form action="calculator.php" method="post">
Current infra level (WITHOUT COMMAS): <input type="text" name="infra" /><br /><br />
Your desired infra level (WITHOUT COMMAS): <input type="text" name="infragoal" /><br /><br />
How many factories do you have?: <input type="text" name="factories" /><br /><br />
<table style="width:25%;">
<tr>
<td colspan="4" style="font: bold 8pt Verdana;"><b>Resources</b></td>
</tr>
<tr>
<td style="font:8pt Verdana;" >Lumber: </td><td><input type="checkbox" name="lumber"/></td>
<td style="font:8pt Verdana;">Iron: </td><td><input type="checkbox" name="iron"/></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Aluminum: </td><td><input type="checkbox" name="aluminium" /></td>
<td style="font:8pt Verdana;">Marble: </td><td><input type="checkbox" name="marble" /></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Coal: </td><td><input type="checkbox" name="coal" /></td>
<td style="font:8pt Verdana;">Steel: </td><td><input type="checkbox" name="steel" /></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Rubber: </td><td><input type="checkbox" name="rubber" /></td>
<td style="font:8pt Verdana;">Construction: </td><td><input type="checkbox" name="construction" /></td>
</tr>
</table>
<br />
<table>
<tr>
<td colspan="4" style="font: bold 8pt Verdana;"><b>Wonders</b></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Interstate System: </td><td><input type="checkbox" name="IS"/></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Scientific Development Center: </td><td><input type="checkbox" name="SDC"/></td>
</tr>
</table>
<br />
<table style="width:25%;">
<tr>
<td colspan="4" style="font: bold 8pt Verdana;"><b>Check the box if your government type is any of the following:</b></td>
</tr>
<tr>
<td style="font:8pt Verdana;">Monarchy, Capitalist, Dictatorship, Federal, Republic or Revolutionary: </td><td><input type="checkbox" name="gov"/></td>
</tr>
</table>
<br />
<input type="submit" value='Submit' />
</form>
</body>
</html>
File name: calculator.php
<html>
<body>
<?php
// Remove commas from infra level, and set vars
$infralevel = $_POST['infra'];
$infragoal = $_POST['infragoal'];
$infradifference = $infragoal - $infralevel;
$factories = $_POST['factories'] * .08;
echo "Your current infra level is: " . $infralevel . "<br />";
echo "Your desired infra level is: " . $infragoal . "<br /><br />";
// Resource percentage total
$ttlresource;
if (isset($_POST['lumber'])) {
$ttlresource += .06;
}
elseif (isset($_POST['iron'])) {
$ttlresource += .05;
}
elseif (isset($_POST['aluminium'])) {
$ttlresource += .07;
}
elseif (isset($_POST['marble'])) {
$ttlresource += .10;
}
elseif (isset($_POST['coal'])) {
$ttlresource += .04;
}
elseif (isset($_POST['steel'])) {
$ttlresource += .02;
}
elseif (isset($_POST['rubber'])) {
$ttlresource += .03;
}
elseif (isset($_POST['construction'])) {
$ttlresource += .05;
}
// Wonders
$wonderttl;
if (isset($_POST['IS'])) {
$wonderttl += .08;
}
elseif (isset($_POST['SDC'])) {
$factories = $_POST['factories'] * .10;
}
// Government
$govttl;
if (isset($_GET['gov'])) {
$govttl += .05;
}
// Infra calc
function calcinfra($currinfra) {
if ($curriinfra < 20) {
$result = 500;
return $result;
}
elseif ($currinfra >= 20 && $currinfra < 100) {
$result = (12 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 100 && $currinfra < 200) {
$result = (15 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 200 && $currinfra < 1000) {
$result = (20 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 1000 && $currinfra < 3000) {
$result = (25 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 3000 && $currinfra < 4000) {
$result = (30 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 4000 && $currinfra < 5000) {
$result = (40 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 5000 && $currinfra < 8000) {
$result = (60 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 8000 && $currinfra < 15000) {
$result = (70 * $currinfra) + 500;
return $result;
}
elseif ($currinfra >= 15000) {
$result = (80 * $currinfra) + 500;
return $result;
}
}
// Add all percentages together
$grandttlpercent = $factories + $govttl + $wonderttl + $ttlresource;
$ttlsaved = $grandttlpercent * (calcinfra($infralevel));
$finalinfracalc = (calcinfra($infralevel));
$ttlcost = $finalinfracalc - $ttlsaved;
setlocale(LC_MONETARY, 'en_US');
echo "To reach " . $infragoal . " you will need to buy " . $infradifference . " infra, which will cost you " . $ttlcost . ".";
?>
</body>
</html>
I've been over it 6 ways to Sunday and still can't figure out what is wrong. I know it's likely something only a newbie would miss, so thanks in advance to anyone who can spot what I'm doing wrong.