[英]How to calculate total price in php
我使用我存储在php数组中的产品ID从db获得产品的价格。
我有一个PHP功能,在我想要持有所有购买的产品。 在这里,我得到购物车项目,我正在调用应该持有总金额的功能。 我传递价值price
,每次当我从购物车中取回产品。
例如,对于第一项迭代,我发送值300,对于第二项400,对于第三项900。
我想把所有这些加起来,并在payableAmount()
获得1600的总价格,我该怎么做?
function getCartitems($product_id){
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id,product_name,product_price,product_image_url FROM product_list WHERE product_id='$product_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td><td>".$row["product_name"]."</td><td><b>₹ </b>".floor($row["product_price"])."</td><td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'><span class='glyphicon glyphicon-remove'></span> remove</a></td></tr>";
$price = $row['product_price'];
payableAmount($price);
}
} else {
echo "There are no such items";
}
$conn->close();
}
function payableAmount($totalPrice){
// calculate total price here
$total = $totalPrice;
echo $total;
}
您只需更新您的查询,而不是像
$total_price = 0;
while($row = $result->fetch_assoc()) {
echo "<tr><td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td><td>".$row["product_name"]."</td><td><b>₹ </b>".floor($row["product_price"])."</td><td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'><span class='glyphicon glyphicon-remove'></span> remove</a></td></tr>";
$total_price += $row['product_price'];
}
只需在外面宣布 -
$total =0;
function payableAmount($totalPrice){
// calculate total price here
global $total;
$total += $totalPrice;
echo $total;
}
使用while循环计算价格。 声明变量$totalPrice
然后在while循环中求和。
function getCartitems($product_id){
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id,product_name,product_price,product_image_url FROM product_list WHERE product_id='$product_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$totalPrice = 0;
while($row = $result->fetch_assoc()) {
echo "<tr><td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td><td>".$row["product_name"]."</td><td><b>₹ </b>".floor($row["product_price"])."</td><td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'><span class='glyphicon glyphicon-remove'></span> remove</a></td></tr>";
$price = $row['product_price'];
$totalPrice += $price;
}
} else {
echo "There are no such items";
}
$conn->close();
}
在函数外部创建变量。 然后在功能中将价格添加到此变量中。 并使用
$total += $totalprice.
你可以简单地在循环中添加价格,并通过将其存储在变量中来获得相同的价格。
paidAmount($ price); //不需要调用不同的函数,因为它只返回当前价格,而不是这个你可以添加价格,如$ total_price + = $ price;
您需要在paidAmount()函数中进行简单修改
function payableAmount(&$totalPrice){
// calculate total price here
$totalPrice += $totalPrice;
}
变量现在通过引用传递,因此它将在您执行后修改,您可以回显$ totalPrice并且它将被更改,其他2个选项是使用静态变量或全局。 但是,如果我是你,我不会使用函数来完成这么简单的任务。 只需在循环和内部循环$total += $price;
之前声明$total
$total += $price;
这就足够了
if ($result->num_rows > 0) {
$totalPrice = 0; // Initialice the var before to go into the while loop
while($row = $result->fetch_assoc()) {
echo "<tr>
<td><img class='cart-image img-responsive' src='".$row["product_image_url"]."'></td>
<td>".$row["product_name"]."</td><td><b>₹ </b>".floor($row["product_price"])."</td>
<td><a href='mycart.php?action=remove&product_id=".$row['product_id']."'>
<span class='glyphicon glyphicon-remove'></span> remove</a></td>
</tr>";
// increment the price...
$totalPrice += $row['product_price'];
}
// Now you can use the $totalPrice var
} else {
echo "There are no such items";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.