简体   繁体   English

使用PHP编写简单的计算器

[英]Using PHP to code a simple calculator

I'm creating a simple BMR calculator that calculates the following formula: BMR = 66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in year ) . 我正在创建一个简单的BMR计算器,该计算器将计算以下公式: BMR = 66 +(6.23 x磅的重量)+(12.7 x英寸的高度)-(6.8 x年龄的年份) This code has been borrowed and modified so I'm not completely comfortable with it. 该代码已被借用和修改,因此我对它不完全满意。

Im curious to as why the '66' is already displayed in answer before I press submit. 我很好奇为什么在按提交之前答案中已经显示了“ 66”。 How can I change my code to not display any values in the answer box until submit is pressed? 如何更改我的代码,直到按下提交时才在答案框中不显示任何值?

The calculator can be found here: http://rgoo.co/calculators/bmr-calculator.php 可以在这里找到计算器: http : //rgoo.co/calculators/bmr-calculator.php

<?php
if (isset($_POST['agev'])) $agev = $_POST['agev'];
if (isset($_POST['feetv'])) $feetv = $_POST['feetv'];
if (isset($_POST['inchesv'])) $inchesv = $_POST['inchesv'];
if (isset($_POST['weightv'])) $weightv = $_POST['weightv'];

$totalheightv = $inchesv + ($feetv*12);
$answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);
echo <<<_END
<form method='post' action='/calculators/bmr-calculator.php'>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>IIFYM test</strong></td></tr>
<tr class="calcrow"><td>AGE:</td><td align="center"><input type='text' name='agev' value="$agev"/>Years</td></tr>
<tr class="calcrow2"><td>Height:</td><td align="center"><input type='text' name='feetv' value="$feetv"/>Ft</td></tr><td align="center"><input type='text' name='inchesv' value="$inchesv"/>In</td></tr>
<tr class="calcrow"><td>Weight:</td><td align="center"><input type='text' name='valuec' value="$valuec"/>lbs</td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td>Your BMR is:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td>
</tr>
</table>
</form>

Default to an empty value for your answer and only calculate when you have all your values: 默认为您的答案为空值,仅当您拥有所有值时才进行计算:

<!doctype html>
<?php
$answer = "";
$agev = "";
$feetv = "";
$inchesv = "";
$weightv = "";

if (isset($_POST['agev']) && isset($_POST['feetv']) && isset($_POST['inchesv']) && isset($_POST['weightv'])) {
    $agev = $_POST['agev'];
    $feetv = $_POST['feetv'];
    $inchesv = $_POST['inchesv'];
    $weightv = $_POST['weightv'];

    $totalheightv = $inchesv + ($feetv*12);
    $answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);

}

echo <<<_END
<form method='post' action=''>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>IIFYM test</strong></td></tr>
<tr class="calcrow"><td>AGE:</td><td align="center"><input type='text' name='agev' value="$agev"/>Years</td></tr>
<tr class="calcrow2"><td>Height:</td><td align="center"><input type='text' name='feetv' value="$feetv"/>Ft</td></tr><td align="center"><input type='text' name='inchesv' value="$inchesv"/>In</td></tr>
<tr class="calcrow"><td>Weight:</td><td align="center"><input type='text' name='weightv' value="$weightv"/>lbs</td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td>Your BMR is:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td>
</tr>
</table>
</form>

This way you ensure that 这样,您可以确保

  • the answer field is empty if nothing was submitted 如果未提交任何内容,则答案字段为空
  • all required values are available to ensure that the BMR is calculated properly 所有必需的值均可用以确保正确计算BMR
  • you get no PHP warnings for using undefined variables 您不会收到有关未定义变量的PHP警告

And I fixed a small issue you had in your version of the code, where you used the name valuec for the weight input field as opposed to weightv in your code. 我固定一个小问题,你在你的代码,您使用的名称的版本有valuec为重输入字段,而不是weightv在你的代码。 And please add any missing HTML tags (like the doctype, <head> , <body> , etc.). 并请添加所有缺少的HTML标记(例如doctype, <head><body>等)。

Do something like 做类似的事情

$answer = '';
if(isset($_POST["usubmit"]) && $_POST["usubmit"] != '')
{
  $answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);
}

Then in the html form add a name to your submit button as "usubmit" 然后在html表单中为您的提交按钮添加一个名称“ usubmit”

<tr class="submit">
   <td colspan="2">
        <input type='submit' name='usubmit'  value='Calculate'/>
   </td>
</tr>

The number 66 is getting shown since on page load it will be assigned to your variable. 显示数字66,因为在页面加载时它将分配给您的变量。 You can avoid this by adding the condition so that it calculates only when the submit is done. 您可以通过添加条件来避免这种情况,以便仅在提交完成后才进行计算。

Quite simply said, before you echo round($answer) you define a mathematical assignment: 简而言之,在回显round($answer)之前,先定义一个数学分配:

$answer = 66 + (6.23*$weightv) + (12.7*$totalheightv) - (6.8*$agev);

Basing the value on 66. So it is already set, when later on in the code you call 该值基于66。因此,稍后在您调用的代码中已经设置了该值

echo($answer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM