Mileage Conversion Script -- code.

See it work here. Copy and use if you want. Leave a link to PHP Stuff, please, if you use this in whole or in part. Written in PHP.


<html>
<head>
<title>Mileage Converter</title>
<META Name="description" Content="Converts mpg (US miles per gallon) to liters per kilometer, and vice versa">
<META Name="keywords" Content="convert, converstion, miles, kilograms, mpg, kpl">
</head>
<body bgcolor=white>
<! -- Mileage v 1.0, Bill Pellowe, bill@eltcalendar.com -->
<?
$form_block = "
<form method  = \"post\" action = \"$PHP_SELF\">
<p>Put a number here:&nbsp;&nbsp;<input type = \"text\" name=\"value1\"  size=10></p>
<p>Change that number...<br>
<select name =\"convert\">
        <option value=\"mpgkpl\" selected>from miles per gallon to kilometers per liter</option>
        <option value=\"kplmpg\">from kilometers per liter to miles per gallon</option>
</select></p>
<p><input type=\"submit\" name=\"submit\" value=\"Do the conversion\"></p>
</form>
 
<p>&nbsp;</p>
<p>&nbsp;<font size=\"-2\"><a href=\"index.php\">up</a></p>
";
 
// remove commas and spaces
trim($value1);
$valuetemp = str_replace(",", "", $value1);
$value = str_replace(" ", "", $valuetemp);
 
// get the first character in the value to  later check  that it's not a letter
$check = substr($value, 0, 1);
 
// in case no number is entered, the page refreshes 
if ($value  == "") {
        echo "<h1>Mileage Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></p></h1>";
        echo "<ul>Jack said this would be useful.</ul>";
        echo "<hr>";
        echo "$form_block";
 
} else if (($check != "0") && ($check != "1") && ($check != "2") && 
($check != "3")  && ($check != "4") && ($check != "5") && ($check != "6") &&
($check != "7") && ($check != "8") && ($check != "9") && ($check != ".") && ($check != "-"))
{
        echo "<h1>Mileage Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></p></h1>";
        echo "<ul>Jack said this would be useful. <strong>It can't start with a letter.</strong></ul>";
        echo "<hr>";
        echo "$form_block";
 
} else {
        {       
                if ($value == 1) {
                        $plural = "";
                        } else 
                $plural = "s"; 
         }  
        if ($convert == "mpgkpl") {
        $result = ($value * 1.6093) / 3.79;
        $unit1 = "mile";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;per gallon (US)";
        $unit2 = "kilometers per liter";
        } else if ($convert == "kplmpg") {
        $result = ($value * 0.6214) / 0.26;
        $unit1 = "kilometer";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;per liter";
        $unit2 = "miles per gallon (US)";
        }
        
echo "<h1>Mileage Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></p></h1>";
echo "<p><strong>Result of your conversion:</strong></p>";
echo "<ul><strong>$value</strong> $unit1 is equal to <strong>";
// format the result (number value, decimal places, decimal separator, thousands separator)
echo number_format($result, "2", ".", ",");
echo "</strong> $unit2.";
if ($check == "-") {
        echo "<br><font color=red><i>What are you doing, driving backwards?</i></font>";
}
echo "</ul></p><hr>";
echo "<p>You can do another conversion if you'd like:</p>";
echo "$form_block";
}
?>
</body>
</html>