[英]How to calculate with html form select and option in php
一种从用户那里获取大量信息并对其进行处理的表格,最后将显示一个数字。 根据其他对象获得许多对象。
index.php
<form action="calc.php" method="POST">
<select name="NumberOfPeople2">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="NumberOfBathTimes">
<option value="1">one</option>
<option value="2">two</option>
</select>
<select name="DurationOfBathing">
<option value="10">10 min</option>
<option value="20">20 min</option>
</select>
<input type="submit" value="submit">
</form>
calc.php
<?php
include 'include/calc.inc.php';
$NumberOfPeople = $_POST['NumberOfPeople'];
$NumberOfBathTimes = $_POST['NumberOfBathTimes'];
if($_POST['DurationOfBathing']=='10')
{
$DurationOfBathing='60';
}
if($_POST['DurationOfBathing']=='20')
{
$DurationOfBathing='100';
}
if($_POST['NumberOfBathTimes']=='1')
{
$FactorUseBath='0.14285714';
$FactorNumberBath='1.2';
}
if($_POST['NumberOfBathTimes']=='2')
{
$FactorUseBath='0.28571429';
$FactorNumberBath='1.2';
}
$ConsumptionBath = new Calc($NumberOfPeople,$DurationOfBathing,$FactorUseBath,$FactorNumberBath);
echo $ConsumptionBath->calcMethod();
calc.inc.php
<?php
class Calc{
public $NumberOfPeople;
public $DurationOfBathing;
public $FactorUseBath;
public $FactorNumberBath;
public function __construct($NumberOfPeople,$DurationOfBathing,$FactorUseBath,$FactorNumberBath){
$this->NumberOfPeople = $NumberOfPeople;
$this->DurationOfBathing; = $DurationOfBathing;
$this->FactorUseBath; = $FactorUseBath;
$this->FactorNumberBath; = $FactorNumberBath;
}
public function calcMethod(){
$result = $this->DurationOfBathing*$this->FactorUseBath;*$this->FactorNumberBath*$this->NumberOfPeople;
return $result;
}
}
获得的所有对象必须相乘。 答案是$ ConsumptionBath这是正确的方法吗?
这是正确的方法,但是您应该在脚本中使用的每个位置声明每个变量。 例如,在给定的脚本,变量$DurationOfBathing
仅在初始化if
条件并给undefined variable
错误的情况下, if
条件失败。 所以我更新了下面的代码:
include 'include/calc.inc.php';
$NumberOfPeople = $_POST['NumberOfPeople'];
$NumberOfBathTimes = $_POST['NumberOfBathTimes'];
$DurationOfBathing = $FactorUseBath = $FactorNumberBath = '';
if ($_POST['DurationOfBathing'] == '10') {
$DurationOfBathing = '60';
} else if ($_POST['DurationOfBathing'] == '20') {
$DurationOfBathing = '100';
}
if ($_POST['NumberOfBathTimes'] == '1') {
$FactorUseBath = '0.14285714';
$FactorNumberBath = '1.2';
} else if ($_POST['NumberOfBathTimes'] == '2') {
$FactorUseBath = '0.28571429';
$FactorNumberBath = '1.2';
}
$ConsumptionBath = new Calc($NumberOfPeople, $DurationOfBathing, $FactorUseBath, $FactorNumberBath);
echo $ConsumptionBath->calcMethod();
更新资料
课堂上也有一些问题。 一些额外的半冒号;
被添加到那里。 使用此更新的代码:
<?php
class Calc {
public $NumberOfPeople;
public $DurationOfBathing;
public $FactorUseBath;
public $FactorNumberBath;
public function __construct($NumberOfPeople, $DurationOfBathing, $FactorUseBath, $FactorNumberBath) {
$this->NumberOfPeople = $NumberOfPeople;
$this->DurationOfBathing = $DurationOfBathing;
$this->FactorUseBath = $FactorUseBath;
$this->FactorNumberBath = $FactorNumberBath;
}
public function calcMethod() {
$result = $this->DurationOfBathing * $this->FactorUseBath * $this->FactorNumberBath * $this->NumberOfPeople;
return $result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.