<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Length Conversion</title>
<!--
<script type="text/javascript" src="myjavascript.js"></script>
-->
<link href="../course.css" rel="stylesheet" type="text/css">
<style type="text/css"><!--
   /* internal styles */
   td.label { text-align: right; } 
   #convform { border: 2px solid gray ; padding: .6em; margin: .6em ; }
--></style>
</head>
<body>
 
<h1>Length Conversion</h1>

<?php


//   Conversion Program
//   Burton Rosenberg
//   May 2007

     
$convNames = array( "inches""feet""miles""centimeters""meters""kilometers""nautical miles" ) ; 
     
$convVals = array( 1.012.063360.00.39370078739.370078739370.07872913.38580 ) ;

     function 
addSelections$a ) {
        for ( 
$i=0$i<count($a); $i++ ) {
           echo 
"<option value=\"$a[$i]\">$a[$i]</option>" ;
        }
     }

     if ( isset(
$_POST["theLength"]) ) {

        
$len $_POST["theLength"] ;
        
$to array_search$_POST["toUnits"], $convNames ) ; 
        
$from array_search$_POST["fromUnits"], $convNames ) ; 

        if ( (!
is_bool($to)) && (!is_bool($from)) ) {
           
$result $len*$convVals[$from]/$convVals[$to] ;
           
// Send the result
?>

     <h2>Conversion Result</h2>

     <p>
     <?=$len?> <?=$convNames[$from]?> equals <?=$result?> <?=$convNames[$to]?>.
     </p>
     <p><em> Thanks for asking!</em></p>
     
<?php
        
}
     }
?>

     <h2>Conversion Query</h2>

     <form action="lenconvert.php" method="POST" >

     <table id="convform"><tr>
     <td class="label">Length:</td><td><input type="text" name="theLength" value=""></td>
     </tr><tr>
     <td class="label">From units:</td><td>
         <select name="fromUnits">
<?php
         addSelections
$convNames ) ;
?>
<!-- to test check of POST values
     <option value="noexists">noexists</option>
-->
         </select>
     </td></tr><tr>
     <td class="label">To units:</td><td>
         <select name="toUnits">
<?php
         addSelections
$convNames ) ;
?>
         </select>
     </td></tr><tr>
     <td class="lable"></td><td><input type="submit" value="Do Conversion"></td>
     </tr></table>

     </form>
  
</body>
</html>