我如何告诉我的php脚本运行一组方程式而不是另一组方程式

时间:2021-09-16 01:07:35

I am building calculators for my website, they are surface area type calculators and I need one metric and one for us. I really don't want to have to have 2 calculators taking up real estate on my page. Instead, I'd like to build a radio button on the top of my form and have the user click either metric or us to generate the proper code. How exactly would I go about doing this? I am just starting to get my hands in php so i apologize if this question has a simple solution.

我正在为我的网站构建计算器,它们是表面积类型计算器,我需要一个度量标准和一个。我真的不想让我的页面上有2个计算器占用房地产。相反,我想在表单顶部构建一个单选按钮,让用户单击公制或我们来生成正确的代码。我到底该怎么做呢?我刚刚开始接触php,所以如果这个问题有一个简单的解决方案我会道歉。

Here is my code:

这是我的代码:

<?php
if (isset($_POST['dia'])) $dia = $_POST['dia'];
if (isset($_POST['wt'])) $wt = $_POST['wt'];
if (isset($_POST['L'])) $L = $_POST['L'];
if (isset($_POST['num'])) $num = $_POST['num'];
if (isset($_POST['waste'])) $waste = $_POST['waste'];
$answer1 = 3.14 * ($dia / 100) * $L * $num ;
$answer2 = 3.14 * (($dia + ($wt * 2)) / 100) * $L * $num;
$answer = ($answer1 + $answer2) * $waste / 200;

echo <<<_END
<form method='post' action='http://www.launchrun.com/consealtest/ConBlockMIC-circ-  metric.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>ConBlock MIC Circular Surface    Area</strong></td></tr>
<tr class="calcrow"><td>Diameter (cm):</td><td align="center"><input type='text' name='dia' value="$dia"/></td></tr>
<tr class="calcrow2"><td>Wall Thickness (cm):</td><td align="center"><input type='text' name='wt' value="$wt"/></td></tr>
<tr class="calcrow"><td>Section Length (meters):</td><td align="center"><input type='text' name='L' value="$L"/></td></tr>
<tr class="calcrow"><td>Number of Sections:</td><td align="center"><input type='text' name='num' value="$num"/></td></tr>
<tr class="calcrow"><td>Waste Variance (%):</td><td align="center"><select name='waste'  value="$waste"/>
<option value="1.05" >0%</option>

  <option value="1.05" >5%</option>

  <option value="1.1" >10%</option>

  <option value="1.2" >20%</option>

  <option value="1.25" >25%</option>

  </select></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td><i>Interior Coating (sq ft):</td>
<td align="center"><input type="text" value="<?php echo round($answer1, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Exteror Coating (sq ft):</td>
<td align="center"><input type="text" value="<?php echo round($answer2, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Total Gallons of ConBlock MIC needed:</td>
<td align="center"><input type="text" value="<?php echo round($answer, 2)?>"></td></i>
</tr>
</table>
</form>

As you can see above I have each answer in a particular equation, the equation for US will just be to replace the #100 with the # 12. Can anyone help me or point me in the right direction? Thanks a lot.

正如你在上面看到的那样,每个答案都在一个特定的等式中,美国的等式只是用#12取代#100。任何人都可以帮助我或指出正确的方向吗?非常感谢。

1 个解决方案

#1


0  

Once you add your radio button or dropdown or whatever it is you choose as a selector for units, its value will get posted along with the rest of the form. Then you can capture its value and use it to make a decision about what that denominator should be.

添加单选按钮或下拉列表或其他任何选择作为单位的选择器后,其值将与表单的其余部分一起发布。然后,您可以捕获其价值并使用它来决定该分母应该是什么。

For example:

<label><input type="radio" name="units" value="US" checked="checked" />US</label>
<label><input type="radio" name="units" value="Metric" />Metric</label>

And then you can adjust your PHP code:

然后你可以调整你的PHP代码:

if (isset($_POST['units'])) $units = $_POST['units'];
...
$denom = ($units == "US" ? 12 : 100); // if $units equals "US", use 12, else 100
$answer1 = 3.14 * ($dia / $denom) * $L * $num ;
$answer2 = 3.14 * (($dia + ($wt * 2)) / $denom) * $L * $num;
...

One other suggestion would be to not hard-code the value of pi. PHP provides the function pi() which returns a much closer approximation of pi than 3.14, which will improve the accuracy of your calculations (note you could also use the constant M_PI):

另一个建议是不要硬编码pi的值。 PHP提供函数pi(),它返回比3.14更接近pi的近似值,这将提高计算的准确性(注意你也可以使用常量M_PI):

$answer1 = pi() * ($dia / $denom) * $L * $num ;
$answer2 = pi() * (($dia + ($wt * 2)) / $denom) * $L * $num;

#1


0  

Once you add your radio button or dropdown or whatever it is you choose as a selector for units, its value will get posted along with the rest of the form. Then you can capture its value and use it to make a decision about what that denominator should be.

添加单选按钮或下拉列表或其他任何选择作为单位的选择器后,其值将与表单的其余部分一起发布。然后,您可以捕获其价值并使用它来决定该分母应该是什么。

For example:

<label><input type="radio" name="units" value="US" checked="checked" />US</label>
<label><input type="radio" name="units" value="Metric" />Metric</label>

And then you can adjust your PHP code:

然后你可以调整你的PHP代码:

if (isset($_POST['units'])) $units = $_POST['units'];
...
$denom = ($units == "US" ? 12 : 100); // if $units equals "US", use 12, else 100
$answer1 = 3.14 * ($dia / $denom) * $L * $num ;
$answer2 = 3.14 * (($dia + ($wt * 2)) / $denom) * $L * $num;
...

One other suggestion would be to not hard-code the value of pi. PHP provides the function pi() which returns a much closer approximation of pi than 3.14, which will improve the accuracy of your calculations (note you could also use the constant M_PI):

另一个建议是不要硬编码pi的值。 PHP提供函数pi(),它返回比3.14更接近pi的近似值,这将提高计算的准确性(注意你也可以使用常量M_PI):

$answer1 = pi() * ($dia / $denom) * $L * $num ;
$answer2 = pi() * (($dia + ($wt * 2)) / $denom) * $L * $num;