<?php
/**
* Example File 2 For Unitconvertor
*
* @version $Id$
* @copyright 2004 CVH chris@cpi-service.com
**/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../styles/1000/standard.css" rel="stylesheet" type="text/css">
<link href="../styles/1000/text.css" rel="stylesheet" type="text/css">
<link href="../styles/1000/boxes.css" rel="stylesheet" type="text/css">
</head>
<body>
<?
require_once("UnitConvertor.php");
class TP_Converter extends UnitConvertor {
function TP_Converter($lang = "en")
{
if ($lang != 'en' ) {
$dec_point = '.'; $thousand_sep = "'";
} else {
$dec_point = ','; $thousand_sep = "'";
}
$this->UnitConvertor($dec_point , $thousand_sep );
} // end func UnitConvertor
function find_base_unit($from,$to) {
while (list($skey,$sval) = each($this->bases)) {
if ($skey == $from || $to == $skey || in_array($to,$sval) || in_array($from,$sval)) {
return $skey;
}
}
return false;
}
function getTable($value, $from_unit, $to_unit, $precision) {
if ($base_unit = $this->find_base_unit($from_unit,$to_unit)) {
// A baseunit was found now lets convert from -> $base_unit
$cell ['value'] = $this->convert($value, $from_unit, $base_unit, $precision)." ".$base_unit;
$cell ['class'] = ($base_unit == $from_unit || $base_unit == $to_unit) ? "framedred": "";
$cells[] = $cell;
// We now have the base unit and value now lets produce the table;
while (list($key,$val) = each($this->bases[$base_unit])) {
$cell ['value'] = $this->convert($value, $from_unit, $val, $precision)." ".$val;
$cell ['class'] = ($val == $from_unit || $val == $to_unit) ? "framedred": "";
$cells[] = $cell;
}
$cc = count($cells);
$string = "<table class=\"framed grayish\" border=\"1\" cellpadding=\"5\" width=\"80%\" align=\"center\"><tr>";
$string .= "<td rowspan=\"$cc\" align=\"center\">$value $from_unit</td>";
$i=0;
foreach ($cells as $cell) {
if ($i==0) {
$string .= "<td class=\"".$cell['class']."\">".$cell['value']."</td>";
$i++;
} else {
$string .= "</tr><tr><td class=\"".$cell['class']."\">".$cell['value']."</td>";
}
}
$string .= "</tr></table>";
return $string;
}
}
}
$conv = new TP_Converter("de");
$conversions = array(
'Temperature'=>array('base' =>'Celsius',
'conv'=>array(
'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),
'Kelvin'=>array('ratio'=>1, 'offset'=>273),
'Reaumur'=>0.8
)
),
'Weight' =>array('base' =>'kg',
'conv'=>array(
'g'=>1000,
'mg'=>1000000,
't'=>0.001,
'grain'=>15432,
'oz'=>35.274,
'lb'=>2.2046,
'cwt(UK)' => 0.019684,
'cwt(US)' => 0.022046,
'ton (US)' => 0.0011023,
'ton (UK)' => 0.0009842,
'Altes Pfund'=>2,
'Zentner'=>0.02,
'Doppelzentner'=>0.01
)
),
'Distance' =>array('base' =>'km',
'conv'=>array(
'm'=>1000,
'dm'=>10000,
'cm'=>100000,
'mm'=>1000000,
'mile'=>0.62137,
'naut.mile'=>0.53996,
'inch(es)'=>39370,
'ft'=>3280.8,
'yd'=>1093.6
)
),
'Area' =>array('base' =>'km²',
'conv'=>array(
'ha'=>100,
'a'=>10000,
'm²'=>pow(1000,2),
'dm²'=>pow(10000,2),
'cm²'=>pow(100000,2),
'mm²'=>pow(1000000,2),
'mile²'=>pow(0.62137,2),
'naut.miles²'=>pow(0.53996,2),
'in²'=>pow(39370,2),
'ft²'=>pow(3280.8,2),
'yd²'=>pow(1093.6,2),
)
),
'Volume' =>array('base' =>'m³',
'conv'=>array(
'in³'=>61023.6,
'ft³'=>35.315,
'cm³'=>pow(10,6),
'dm³'=>1000,
'litre'=>1000,
'hl'=>10,
'yd³'=>1.30795,
'gal(US)'=>264.172,
'gal(UK)'=>219.969,
'pt (UK)'=>1000/0.56826,
'barrel petrolium'=>1000/158.99,
'Register Tons'=>2.832,
'Ocean Tons'=>1.1327
)
),
'Speed' =>array('base' =>'kmph',
'conv'=>array(
'mps'=>1/3.6,
'milesph'=>0.62137,
'kn'=>0.53996
)
)
);
while (list($key,$val) = each($conversions)) {
$conv->addConversion($val['base'], $val['conv']);
$list[$key][] =$val['base'];
while (list($ukey,$uval) = each($val['conv'])) {
$list[$key][] = $ukey;
}
}
$options='';
reset($list);
while(list($key,$val) = each($list)) {
$options .= "\n\t<optgroup label=\"$key\">";
while(list($ukey,$uval) = each($val)) {
$options .= "\n\t\t<option value=\"$uval\">$uval</option>";
}
$options .= "\n\t</optgroup>";
}
if (isset($_POST['from_unit']) && isset($_POST['value'])) {
$_POST['value'] = $_POST['value'] + 0;
print "<br>";
print "<p> Direct Conversion : ". $_POST['value'] . " " .$_POST['from_unit']. " = ".$conv->convert($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5). " ".$_POST['to_unit']."</p>";
print "<br>";
print "<p>Conversion Table : ";
print ($conv->getTable($_POST['value'], $_POST['from_unit'], $_POST['to_unit'], 5))."</p>";
} else {
print "<p>please specify</p>";
}
?>
<br><div class="framed">
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post" name="conversion<?php print $i; ?>" id="to_unit">
<h3>Conversions</h3>
<input name="value" type="text" id="value" value="" size="10" maxlength="10">
<select name="from_unit">
<option selected="selected">Not Selected</option>
<?php print $options ?>
</select> to
<select name="to_unit">
<option selected="selected">Not Selected</option>
<?php print $options ?>
</select>
<input type="submit" name="Submit" value="Submit">
</form></div>
</body>
</html>
|