0

I'm having a hell of a time creating a PHP calculator for my work's website for a special needs transportation service (shuttle buses) I have the HTML and PHP set up, but I can't seem to get it to work.

These are what i need to figure out how to make the PHP do, so I can put it on the services page. I can't seem to get it right.

Constant rate: $50 for 30min of Trans Additional Set cost: 5% taxed on

Here's the HTML

<form method="get" action="calculationtable.php">
    <p>Time Traveled:
    <input type="text" name="hours" id="hours" />
    </p>
    <p>Transport Rate ($100/1hr):
    <input type="text" name="rate" id="rate" /><br /><br />
    <input type="submit" />
    </p>

</form>

and the PHP

<?php

$hours = (int) $_GET["hours"];
$rate = (int) $_GET["rate"];

$overtime = max($hours - 40, 0);
$pay += $overtime * $rate * 1.5;
$pay += ($hours - $overtime) * $rate;

echo "Hours Traveled: " . $hours . "<br>";
echo "Transport rate (per hour): $" . number_format($rate, 2) . "<br>";
echo "Additional Hours: " . $overtime . "<br>";
echo "Your Total is: $" . number_format($pay, 2) . "<br>";
?>

2 Answers2

0

A couple of things, initialize $pay as @AeroX stated.

(float)$pay = 0;

... then increment it. Also, although PHP does really well "figuring out what you mean", I choose not to depend on it. i.e.:

(int)$hours = 0;
(int)$rate = 0;
if(isset($_GET['hours']) && is_numeric($_GET['hours'])) $hours += filter_input(INPUT_GET, 'hours');
if(isset($_GET['rate']) && is_numeric($_GET['rate'])) $rate += filter_input(INPUT_GET, 'rate');
bt3of4
  • 36
  • 6
0

As others have mentioned, turn on error reporting to debug. As @AeroX said, you need to initialize the variable before you can increment.

Here's a working example with the tax part added in

$hours = 0.5;
$rate = 100;

$overtime = max($hours - 40, 0);
$pay = $overtime * $rate * 1.5;
$pay += ($hours - $overtime) * $rate;
$pay = $pay + ($pay * 0.05);

echo "Hours Traveled: " . $hours . "<br>";
echo "Transport rate (per hour): $" . number_format($rate, 2) . "<br>";
echo "Additional Hours: " . $overtime . "<br>";
echo "Your Total is: $" . number_format($pay, 2) . "<br>";
bmcculley
  • 2,048
  • 1
  • 14
  • 17