<html>
<head>
<title>
Calculate Mileage
</title>
<STYLE TYPE="text/css">
TD{font-size: 30px;}
BODY{font-size: 30px;}
</STYLE>
<script>
function myThing(url) {
window.open(url, "pictureWindow", "width=1600, height=1000");
}
</script>
</head>
<body>
<h1>Your Gas Mileage is:</h1>
<?php
$file = "mileage.csv";
if ($_POST['type'] == 'submit') {
$odo = $_POST['odo'];
$litres = $_POST['litres'];
$costpl = $_POST['cost'];
$car = $_POST['cars'];
if ($car == "prius") {
$carImage = "prius.jpg";
$file = "mileagePrius.csv";
} else {
$carImage = "pathfinder.jpg";
$file = "mileagePathfinder.csv";
}
if ($litres < 0) {
$litres = -$litres;
$litres = $litres * 3.78541;
$costpl = $costpl / 3.78541;
}
$cost = round($litres * $costpl/100, 2);
$location = $_POST['location'];
$time = $_POST['time'];
$fh = fopen($file, 'r');
$nl = "*_*_*";
do {
$line = $nl;
} while (($nl = fgets($fh)) != "");
$lineVals = explode(',', $line);
$km = $odo - $lineVals[3];
$imp_gallons = $litres / 4.546;
$miles = $km/1.609;
$litresPer100Km = round($litres / $km * 100, 2);
$mpg = round($miles/$imp_gallons, 0);
echo "<img src=$carImage alt=\"\" width=\"300\" height=\"175\"><br>";
echo "Kilometers = $km<br>";
echo "Cost = $cost<br>";
echo "Litres per 100 kilometers = $litresPer100Km <br>";
echo "Miles per gallon = $mpg <br><br>";
echo "Cost per kilometer = $" . round($cost/$km, 2) . "<br>";
echo "Cost per mile = $" . round($cost/$miles, 2) . "<br><br>";
echo "Time: " . $time . "<br>";
echo "Location = $location<br>";
if ($output = fopen ($file, 'a')) {
$cpk = round($cost/$km, 2);
$cpm = round($cost/$miles, 2);
list($lat, $long) = explode(',',$location);
$url = "maps.google.com/maps?z=18&t=m&q=loc:$lat+$long";
fputs ($output, "$time,$litres,$costpl,$odo,$km,$cost,$litresPer100Km,$mpg,$cpk,$cpm,$url\n");
fclose($output);
} else {
echo "Could not open data file for append.";
}
}
if ($_POST['type'] == 'display') {
$car = $_POST['cars'];
if ($car == "prius") {
$carImage = "prius.jpg";
$file = "mileagePrius.csv";
} else {
$carImage = "pathfinder.jpg";
$file = "mileagePathfinder.csv";
}
echo "<img src=$carImage alt=\"\" width=\"300\" height=\"175\"><br>";
if($input = fopen ($file, 'r')) {
echo "<table border=\"2px\"><b><tr><td>time</td><td>litres</td><td>cost/l</td><td>Odo</td><td>km</td><td>cost</td><td>l/100km</td><td>mpg</td><td>$/km</td><td>$/mile</td><td>map</td></tr></b>";
while (!feof($input)) {
$line = fgets($input);
if ($line) {
list($time,$litres,$costpl,$odo,$km, $cost, $litresPer100Km, $mpg, $cpk, $cpm, $url) = explode(',', $line);
$url = trim($url);
$url = "https://$url";
$map = "<button onclick=\"myThing('" . $url . "')\"><img src=\"map.jpeg\"></button>";
echo "<tr><td>$time</td><td>$litres</td><td>$costpl</td><td>$odo<td>$km</td><td>$cost</td><td>$litresPer100Km</td><td>$mpg</td><td>$cpk</td><td>$cpm</td><td>$map</td></tr>\n";
}
}
echo "</table>";
} else {
echo "Could not open data file for read.";
}
}
?>
</body>
</html>