Jeg har lavet en lille klasse der burde gøre det for dig...
converter.class.php:
<?php
class ConvertPrefix {
protected $PrefixFaktor = array("-1","-2","-3","-6","-9","-12","-15","-18","-21","-24","0","1","2","3","6","9","12","15","18","21","24");
protected $PrefixName = array("deci","centi","milli","micro","nano","pico","femto","atto","zepto","yocto","","deca","hecto","kilo","mega","giga","tera","peta","exa","zetta","yotta");
protected $PrefixSymbol = array("d","c","m","µ","n","p","f","a","z","y","","da","h","k","M","G","T","P","E","Z","Y");
protected $WordFrom = "From";
protected $WordTo = "To";
protected $FaktorName;
//CONSTRUCTOR
public function __construct($FaktorName = "Value")
{
$this->FaktorName = $FaktorName;
}
public function ShowForm()
{
$Output = "<form action='' method='POST'>\n";
$Output .= "<input type='text' name='PrefixValue'/><br>\n";
$Output .= $this->WordFrom.": ".$this->ShowSelect($this->WordFrom)."<br>\n";
$Output .= $this->WordTo.": ".$this->ShowSelect($this->WordTo)."<br>\n";
$Output .= "<input type='submit' value='Go'/>\n";
$Output .= "</form>\n";
$Output .= $this->Convert();
return $Output;
}
private function ShowSelect($Type)
{
$Output = "<select name='".$Type."'>\n";
$Output .= "<option value=''>-vælg enhed-</option>\n";
$x = 0;
while($x <= count($this->PrefixFaktor)-1){
$Selected = "";
if($_POST[$Type] == $x){$Selected = "selected";}
$Output .= "<option value='".$x."' ".$Selected.">".ucfirst($this->PrefixName[$x].$this->FaktorName)." (".$this->PrefixFaktor[$x].")</option>\n";
$x++;
}
$Output .= '</select>';
return $Output;
}
private function Convert()
{
$FromValue = pow(10, $this->PrefixFaktor[$_POST[$this->WordFrom]]) * $_POST['PrefixValue'];
$ToValue = pow(10, $this->PrefixFaktor[$_POST[$this->WordTo]]);
if($this->PrefixFaktor[$_POST[$this->WordTo]] < 0){
$Output = $FromValue / $ToValue;
}else{
$Output = $ToValue * $FromValue;
}
return $Output." ".ucfirst($this->PrefixName[$_POST[$this->WordTo]].$this->FaktorName);
}
}
?>
example.php:
<?php
include("converter.class.php");
//START CONVERTER CLASS
$Convert = new ConvertPrefix("meter");
echo $Convert->ShowForm();
?>
Det overstående er ikke testet totalt gennem...
Indlæg senest redigeret d. 30.04.2010 10:30 af Bruger #7728