我正在尝试使用基本和简单代码转换测量的PHP转换器(新的)。我希望这张图片可以帮助您了解我正在尝试做的事情:
我创建了一个 HTML文件,其表单如下所示:
<form action="converter.php" method="post">
<input type="text" name="number">
<select name="choose">
<option>Choose</option>
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
<input type="submit" value="Convert!">
</form>
PHP文件看起来像这样,而且不是很多,但是:
<?php
$number = $_POST['number'];
$choose = $_POST['choose'];
$x = $number;
echo "$number $choose <br>";
?>
Aaaand我被困住了。我真的不知道如何在数学和代码中转换值。如果你给我整个代码或下一步做什么都没关系,只需要朝着正确的方向推进。
谢谢!
答案 0 :(得分:0)
在php文件中进行必要的计算。
注意:编辑了计算,但从未测试过。仔细检查计算部分。
<强> HTML 强>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//called when key is pressed in textbox
$("#number").keypress(function (e) {
//if the letter is not digit then display error and don\'t type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
});
});
</script>
<form action="converter.php" method="post">
<input type="text" name="number" id="number">
<select name="choose">
<option>Choose</option>
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
<input type="submit" value="Convert!">
</form>
<强> converter.php 强>
<?php
$number = isset($_POST['number']) ? $_POST['number'] : 0;
$choose = isset($_POST['choose']) ? $_POST['choose'] : '';
switch ($choose)
{
case 'meter':
$cm = $number * 100;
$foot = $number * 3.2808;
$inches = $number * 39.370;
$Answer = 'Meters: '.$number.'<br />'.
'Cm: '.$cm.'<br />'.
'Foot: '.$foot.'<br />'.
'Inches: '.$inches;
break;
case 'cm':
$meter = $number / 100;
$foot = $number * 0.032808;
$inches = $number * 0.39370;
$Answer = 'Cm: '.$cm.'<br />'.
'Meters: '.$number.'<br />'.
'Foot: '.$foot.'<br />'.
'Inches: '.$inches;
break;
case 'foot':
$meter = $number / 3.2808;
$cm = $number / 0.032808;
$inches = $number * 12.000;
$Answer = 'Foot: '.$foot.'<br />'.
'Meters: '.$number.'<br />'.
'Cm: '.$cm.'<br />'.
'Inches: '.$inches;
break;
case 'inches':
$meter = $number / 39.370;
$cm = $number / 0.39370;
$foot = $number / 12.000;
$Answer = 'Inches: '.$inches.'<br />'.
'Foot: '.$foot.'<br />'.
'Meters: '.$number.'<br />'.
'Cm: '.$cm;
break;
default:
$Answer = '';
break;
}
echo $Answer;
?>
答案 1 :(得分:0)
在不知道输入单元类型的情况下,您将要做的就是猜测。我的建议是将表单更改为以下代码以便更简单地输入。然后,它是一个快速简单的除法/乘法,以获得所需的结果。这是表单和php处理代码的快速代码示例。
还有一些代码可以添加到其中,包括额外的转换和快速检查以确定某些值已设置,但您应该充分了解它的主旨。
<form action="converter.php" method="post">
<fieldset><legend>Select Your Input Type and Value</legend>
<input type="number" name="inputNumber" step="any" value="" />
<select name="inputFormat">
<optgroup name="Choose">
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
</optgroup>
</select>
</fieldset>
<fieldset><legend>Select your Output Type</legend>
<select name="outputFormat">
<optgroup name="Choose">
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
</optgroup>
</select>
</fieldset>
<input type="submit" name="submitForm" value="Convert!" />
</form>
<?php
$outputNumber=0;
$inputNumber = (float)$_POST['inputNumber'];
$inputFormat = $_POST['inputFormat'];
$outputFormat = $_POST['outputFormat'];
switch($inputFormat){
case "meter":
switch ($outputFormat){
case "meter": $outputNumber = $inputNumber;break; //METER TO METER
case "cm": $outputNumber = $inputNumber*100;break; //METER TO CM
case "foot": $outputNumber = $inputNumber*3.2808399;break; //METER TO FT
case "inches": $outputNumber = $inputNumber*39.3700787;break;//METER TO INCHES
}
break;
case "cm":
//ADD CONVERSION SWITCH IN HERE
break;
case "foot":
//ADD CONVERSION SWITCH IN HERE
break;
case "inches":
//ADD CONVERSION SWITCH IN HERE
break;
}
echo $outputNumber;
?>
答案 2 :(得分:0)
更简单:
<强>的index.html 强>
<form action="converter.php" method="post">
<input type="number" name="number" required>
<select name="choose">
<option>Choose</option>
<option value="meter" id="meter">Meter</option>
<option value="cm" id="cm">Cm</option>
<option value="foot" id="fot">Fot</option>
<option value="inches" id="tum">Tum</option>
<input type="submit" value="Convert!">
</form>
<强> converter.php 强>
<?php
$number = $_POST['number'];
$choose = $_POST['choose'];
$meters = $number / ($choose == 'cm'? 100 : ($choose == 'foot'? 3.28084 : ($choose == 'inches'? 39.3701 : 1)));
echo "Meters : $meters<br/>";
echo "Cm : ".($meters*100)."<br/>";
echo "Foot : ".($meters*3.28084)."<br/>";
echo "Inches : ".($meters*39.3701)."<br/>";
答案 3 :(得分:0)
也请针对这些类别更新php代码。 <select name="choose"><option>Choose</option><option value="kg" id="kg">kg</option><option value="liter" id="liter">Liter</option><option value="mililiter" id="mililiter">Mililiter</option></select>
答案 4 :(得分:0)
这个问题已经回答了,但是为了将来起见,我特别为那些将要进行更大范围的SI单位转换的人写这篇文章。
我编写了一个用于SI单位转换的PHP库puconverter,您只需调用一个函数即可像这样转换它
<?php
include "puconverter.php";
echo length_converter($val,"cm", "m"); //this will convert $val in centimetres to its equivalent metres
?>
在您的senerio中,使用它来创建Web应用程序:传递的SI单位参数将是一个变量,该变量以HTML形式保存所选单位的名称值,在这种情况下,我们假设该值将为cm,因此像这样使用:
if(isset($_POST["submit"])){
$convertFrom= $_POST["centimeter"];
$val= $_POST["value"];
//$val is value to convert, $convertFrom is unit of the value to be converted
echo length_converter($val $convertFrom,"m");//returns value in metres
echo length_converter($val ,$convertFrom,"mm");//returns value in millimetres
echo length_converter($val ,$convertFrom,"km");//returns value in kilometres
}
有关所有其他SI单位转换功能的完整文档,请访问库GitHub页面:https://github.com/GeniusGeeek/pu-converter/blob/master/README.md