Skip to content

Commit 7e40d94

Browse files
committed
adding volume conversion page and functions
1 parent 2bafea3 commit 7e40d94

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

includes/volume.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
require_once('units.php');
3+
4+
function convertToLiters($value, $fromUnit)
5+
{
6+
if (array_key_exists($fromUnit, VOLUME_UNITS)) {
7+
return $value * VOLUME_UNITS[$fromUnit];
8+
} else {
9+
return "Unsupported unit";
10+
}
11+
}
12+
13+
function convertFromLiters($value, $toUnit)
14+
{
15+
if (array_key_exists($toUnit, VOLUME_UNITS)) {
16+
return $value / VOLUME_UNITS[$toUnit];
17+
} else {
18+
return "Unsupported unit";
19+
}
20+
}
21+
22+
function convertVolume($value, $fromUnit, $toUnit)
23+
{
24+
$literValue = convertToLiters($value, $fromUnit);
25+
$newValue = convertFromLiters($literValue, $toUnit);
26+
27+
return $newValue;
28+
}
29+
?>

volume.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
require_once('includes/volume.php');
4+
require_once('includes/camelCase.php');
5+
6+
// Default empty variables
7+
$fromValue = '';
8+
$fromUnit = '';
9+
$toUnit = '';
10+
$toValue = '';
11+
12+
/* Check conversion request on submit */
13+
if ($_POST['submit']) {
14+
$fromValue = $_POST['fromValue'];
15+
$fromUnit = $_POST['fromUnit'];
16+
$toUnit = $_POST['toUnit'];
17+
18+
$toValue = convertVolume($fromValue, $fromUnit, $toUnit);
19+
}
20+
21+
$volumeUnits= array(
22+
'cubic inches',
23+
'cubic feet',
24+
'UK gallons',
25+
'UK quarts',
26+
'UK pints',
27+
'UK cups',
28+
'UK ounces',
29+
'UK tablespoons',
30+
'UK teaspoons',
31+
'US gallons',
32+
'US quarts',
33+
'US pints',
34+
'US cups',
35+
'US ounces',
36+
'US tablespoons',
37+
'US teaspoons',
38+
'cubic centimeters',
39+
'cubic meters',
40+
'liters',
41+
'milliliters'
42+
);
43+
?>
44+
<!DOCTYPE html>
45+
<html>
46+
<head>
47+
<meta charset="UTF-8">
48+
<title>Convert Volume</title>
49+
<link href="styles.css" rel="stylesheet" type="text/css">
50+
</head>
51+
<body>
52+
53+
<div id="main-content">
54+
55+
<h1>Convert Volume</h1>
56+
57+
<form action="" method="post">
58+
59+
<div class="entry">
60+
<label>From:</label>&nbsp;
61+
<input type="text" name="fromValue" value="<?php { echo $fromValue;} ?>" />&nbsp;
62+
<select name="fromUnit">
63+
<?php
64+
foreach ($volumeUnits as $unit) {
65+
echo "<option value=\"" . camelCase($unit) . "\"";
66+
if ($fromUnit == 'cubeInches') {
67+
{
68+
echo " selected";
69+
}
70+
}
71+
echo ">" . ucwords($unit) . "</option>";
72+
}
73+
?>
74+
</select>
75+
</div>
76+
77+
<div class="entry">
78+
<label>To:</label>&nbsp;
79+
<input type="text" name="toValue" value="<?php { echo $toValue;} ?>" />&nbsp;
80+
<select name="toUnit">
81+
<?php
82+
foreach ($volumeUnits as $unit) {
83+
echo "<option value=\"" . camelCase($unit) . "\"";
84+
if ($toUnit == 'cubeInches') {
85+
{
86+
echo " selected";
87+
}
88+
}
89+
echo ">" . ucwords($unit) . "</option>";
90+
}
91+
?>
92+
</select>
93+
94+
</div>
95+
96+
<input type="submit" name="submit" value="Submit" />
97+
</form>
98+
99+
<br/>
100+
<a href="index.php">Return to menu</a>
101+
102+
</div>
103+
</body>
104+
</html>

0 commit comments

Comments
 (0)