/
vqg-calculator.php
88 lines (68 loc) · 3.28 KB
/
vqg-calculator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php # Script 3.5 - calculator.php
// This function creates a radio button.
// This functions takes one argument: the value.
// The function also makes the button 'sticky'.
function create_radio($value, $name = 'gallon_price') {
// start the element:
echo '<input type="radio" name="' . $name . '" value="' . $value . '"';
// Check for stickiness
if (isset($_POST[$name]) && ($_POST[$name] == $value)) {
echo ' checked="checked"';
}
// Complete the element
echo " /> $value ";
} // End of create_gallon_radio() function.
// This function calculates the cost of the trip.
// The function takes three arguments: the distance, the fuel efficiency, and the price per gallon.
// The function returns the total cost.
function calculate_trip_cost($miles, $mpg, $ppg) {
// Get the cost of those gallons;
$gallons = $miles/$mpg;
// Get the cost of those gallons:
$dollars = $gallons/$ppg;
// Return the formatted cost:
return number_format($dollars, 2);
} // End of calculate_trip_cost() function.
$page_title = 'Trip Cost Calculator';
include ('includes/header.html');
// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Minimal form validation:
if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) &&
is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) {
// Calculate the results:
$cost = calculate_trip_cost($_POST['distance'], $_POST['efficiency'], $_POST['gallon_price']);
$hours = $_POST['distance']/70;
// Print the results:
echo '<h1>Total Estimated Cost</h1>'
. '<p>The total cost of driving ' . $_POST['distance'] . ' miles, averaging '
. $_POST['efficiency'] . ' miles per gallon, and paying an average of $'
. $_POST['gallon_price'] . ' per gallon, is $' . $cost
. '. If you drive at an average of 65 miles per hour, the trip will take approximately '
. number_format($hours, 2) . ' hours.</p><br />';
} else { // Invalid submitted values.
echo '<h1>Error!</h1>
<p class="error">Please enter a valid distance, price per gallon, and fuel efficiency.</p>';
}
} // End of main submission IF.
// Leave the PHP section and create the HTML form:
?>
<h1>Trip Cost Calculator</h1>
<form action="vgqch3-calculator.php" method="post">
<p>Distance (in miles): <input type="text" name="distance" value="<?php if (isset($_POST['distance'])) echo $_POST['distance']; ?>" /></p>
<p>Ave. Price Per Gallon: <span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span></p>
<p>Fuel Efficiency: <select name="efficiency">
<option value="10" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '10')) echo 'selected="selected"'; ?> >Terrible</option>
<option value="20" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '20')) echo 'selected="selected"'; ?> >Decent</option>
<option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo 'selected="selected"'; ?> >Very Good</option>
<option value="50" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '50')) echo 'selected="selected"'; ?> >Outstanding</option>
</select></p>
<p><input type="submit" name="submit" value="Calculate!" /></p>
</form>
<?php include ('includes/footer.html'); ?>