Skip to content

Commit 098dac2

Browse files
committed
adding speed conversion page and functions, including additional unit of knots
1 parent 2003d1c commit 098dac2

File tree

5 files changed

+124
-3
lines changed

5 files changed

+124
-3
lines changed

includes/floatToString.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Function to convert scientic notation of a very large or
55
* very small number into a readable float */
6-
function floatToString($float, $precision = 20)
6+
function floatToString($float, $precision = 10)
77
{
88
$float = (float) $float; // Confirm variable is float
99
$string = number_format($float, $precision, '.', ''); // Format number

includes/speed.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
require_once('length.php');
3+
require_once('units.php');
4+
require_once('floatToString.php');
5+
6+
function convertSpeed($value, $fromUnit, $toUnit)
7+
{
8+
if ($fromUnit == 'knots') {
9+
$fromUnit = 'nauticalMilesPerHour';
10+
}
11+
12+
if ($toUnit == 'knots') {
13+
$toUnit = 'nauticalMilesPerHour';
14+
}
15+
16+
list($fromDist, $fromTime) = explode('Per', $fromUnit);
17+
list($toDist, $toTime) = explode('Per', $toUnit);
18+
19+
if ($fromTime == 'Hour') {
20+
$value /= 3600;
21+
}
22+
23+
$value = convertLength($value, $fromDist, $toDist);
24+
25+
if ($toTime == 'Hour') {
26+
$value *= 3600;
27+
}
28+
29+
return floatToString($value);
30+
}
31+
?>

includes/units.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"kilometers" => 1000,
1111
"acres" => 1000,
1212
"hectares" => 1000,
13+
"nauticalMiles" => 1852
1314
);
1415

1516
const VOLUME_UNITS = array(

mass.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<?php
5353
foreach ($massUnits as $unit) {
5454
echo "<option value=\"" . camelCase($unit) . "\"";
55-
if ($fromUnit == $unit) {
55+
if ($fromUnit == camelCase($unit)) {
5656
{
5757
echo " selected";
5858
}
@@ -70,7 +70,7 @@
7070
<?php
7171
foreach ($massUnits as $unit) {
7272
echo "<option value=\"" . camelCase($unit) . "\"";
73-
if ($toUnit == $unit) {
73+
if ($toUnit == camelCase($unit)) {
7474
{
7575
echo " selected";
7676
}

speed.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
require_once('includes/speed.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 = convertSpeed($fromValue, $fromUnit, $toUnit);
19+
}
20+
21+
$speedUnits = array(
22+
"feet per second",
23+
"miles per hour",
24+
"meters per second",
25+
"kilometers per hour",
26+
"knots"
27+
);
28+
?>
29+
<!DOCTYPE html>
30+
<html>
31+
<head>
32+
<meta charset="UTF-8">
33+
<title>Convert Speed</title>
34+
<link href="styles.css" rel="stylesheet" type="text/css">
35+
</head>
36+
<body>
37+
38+
<div id="main-content">
39+
40+
<h1>Convert Speed</h1>
41+
42+
<form action="" method="post">
43+
44+
<div class="entry">
45+
<label>From:</label>&nbsp;
46+
<input type="text" name="fromValue" value="<?php { echo $fromValue;} ?>" />&nbsp;
47+
<select name="fromUnit">
48+
<?php
49+
foreach ($speedUnits as $unit) {
50+
echo "<option value=\"" . camelCase($unit) . "\"";
51+
if ($fromUnit == camelCase($unit)) {
52+
{
53+
echo " selected";
54+
}
55+
}
56+
echo ">" . ucwords($unit) . "</option>";
57+
}
58+
?>
59+
</select>
60+
</div>
61+
62+
<div class="entry">
63+
<label>To:</label>&nbsp;
64+
<input type="text" name="toValue" value="<?php { echo $toValue;} ?>" />&nbsp;
65+
<select name="toUnit">
66+
<?php
67+
foreach ($speedUnits as $unit) {
68+
echo "<option value=\"" . camelCase($unit) . "\"";
69+
if ($toUnit == camelCase($unit)) {
70+
{
71+
echo " selected";
72+
}
73+
}
74+
echo ">" . ucwords($unit) . "</option>";
75+
}
76+
?>
77+
</select>
78+
79+
</div>
80+
81+
<input type="submit" name="submit" value="Submit" />
82+
</form>
83+
84+
<br/>
85+
<a href="index.php">Return to menu</a>
86+
87+
</div>
88+
</body>
89+
</html>

0 commit comments

Comments
 (0)