我如何使用 php 从 select 的用户输入计算总价
How can i calculate the total price from the user input from select from using php
我想计算来自 $_SESSION 的用户输入的总价格。
但是,我的代码似乎有问题。
<label>Seats STA:</label>
<select name="seats[STA]" id="STA">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<span class="error">* Please select atleast 1 seat <?php echo $seatErr ;?></span>
$_SESSION['seats']['STA']=$_POST['seats']['STA'];
$sta = 15;
$stp = 5;
$stc = 7.5;
$fca = 30;
$fcp = 20;
$fcc = 15;
$totalamount = "0.0";
$totalamount = $_SESSION['seats']['STA'] * $sta +
$_SESSION['seats']['STP'] * $stp +
$_SESSION['seats']['STC'] * $stc +
$_SESSION['seats']['FCA'] * $fca +
$_SESSION['seats']['FCP'] * $fcp +
$_SESSION['seats']['FCC'] * $fcc;
$tax = 0.1;
$totalprice = $totalamount * (1+$tax);
number_format($totalprice,2);
我的问题是,
$sta
是省钱的正确方法吗?
如果没有,我可以在哪里保存select表格中的产品价格??
number_format($totalprice,2);
是显示 2 位小数的正确方法吗??
感谢阅读我的代码!!
您可以将价格保存在数组中。例如,您也可以从 JSON 文件或数据库中读取。
用户不应发送价格。
如果你想显示价格是,但如果你想继续计算价值,round($total_price, 2)
是更合适的方法。
代码:
$_PRICES = array(
"STA" => 15,
"STP" => 5,
"STC" => 7.5,
"FCA" => 30,
"FCP" => 20,
"FCC" => 15
);
$total_price = 0;
foreach ($_SESSION['seats'] as $item => $amount) {
$price = $_PRICES[$item];
if ($price != null) {
$total_price += $amount * $price;
}
}
$tax = 0.1;
$total_price *= 1+$tax;
echo number_format($total_price,2);
我想计算来自 $_SESSION 的用户输入的总价格。 但是,我的代码似乎有问题。
<label>Seats STA:</label>
<select name="seats[STA]" id="STA">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<span class="error">* Please select atleast 1 seat <?php echo $seatErr ;?></span>
$_SESSION['seats']['STA']=$_POST['seats']['STA'];
$sta = 15;
$stp = 5;
$stc = 7.5;
$fca = 30;
$fcp = 20;
$fcc = 15;
$totalamount = "0.0";
$totalamount = $_SESSION['seats']['STA'] * $sta +
$_SESSION['seats']['STP'] * $stp +
$_SESSION['seats']['STC'] * $stc +
$_SESSION['seats']['FCA'] * $fca +
$_SESSION['seats']['FCP'] * $fcp +
$_SESSION['seats']['FCC'] * $fcc;
$tax = 0.1;
$totalprice = $totalamount * (1+$tax);
number_format($totalprice,2);
我的问题是,
$sta
是省钱的正确方法吗?如果没有,我可以在哪里保存select表格中的产品价格??
number_format($totalprice,2);
是显示 2 位小数的正确方法吗??
感谢阅读我的代码!!
您可以将价格保存在数组中。例如,您也可以从 JSON 文件或数据库中读取。
用户不应发送价格。
如果你想显示价格是,但如果你想继续计算价值,
round($total_price, 2)
是更合适的方法。
代码:
$_PRICES = array(
"STA" => 15,
"STP" => 5,
"STC" => 7.5,
"FCA" => 30,
"FCP" => 20,
"FCC" => 15
);
$total_price = 0;
foreach ($_SESSION['seats'] as $item => $amount) {
$price = $_PRICES[$item];
if ($price != null) {
$total_price += $amount * $price;
}
}
$tax = 0.1;
$total_price *= 1+$tax;
echo number_format($total_price,2);