php 如何在php中计算总价
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32986549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate total price in php
提问by CJAY
I am getting the price of the product from db using the product id i have stored in php array.
我使用存储在 php 数组中的产品 ID 从 db 获取产品的价格。
i have a php function where in i want to hold a total of all the products purchased. here i am getting the cart items and i am calling the function which should hold total amount. i am passing the value price
each time when i retrieve the product from cart.
我有一个 php 函数,我想在其中保存所有购买的产品。在这里,我正在获取购物车项目,并且正在调用应保存总金额的函数。price
每次从购物车中检索产品时,我都会传递该值。
for example for first item iteration i send value 300, and for second 400, for third 900.
例如,对于第一个项目迭代,我发送值 300,第二个 400,第三个 900。
i want to add up all these and get total price of 1600 in payableAmount()
how can i do this?
我想把所有这些加起来得到 1600 的总价,payableAmount()
我该怎么做?
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;
}
回答by Narendrasingh Sisodia
You just simply update your query instead like as
您只需简单地更新您的查询,而不是像
$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'];
}
回答by Rohit Kumar
Just declare total outside -
只需在外面声明总 -
$total =0;
function payableAmount($totalPrice){
// calculate total price here
global $total;
$total += $totalPrice;
echo $total;
}
回答by Akshay
Use the while loop to calculate the price. Declare a variable $totalPrice
and then sum it within the while loop.
使用 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();
}
回答by M.Zuberbühler
Create a Variable outside the function. Then you add the price to this variable in the funtion. and use
在函数外创建一个变量。然后在函数中将价格添加到该变量中。并使用
$total += $totalprice.
回答by Niraj Karmick
you can simply add the price in loop and get the same by storing it in variable.
您可以简单地在循环中添加价格并通过将其存储在变量中来获得相同的价格。
payableAmount($price);//Not Need to call different function as it return only the current price,instead of this you can add the price as given below $total_price+=$price;
payableAmount($price);//不需要调用不同的函数,因为它只返回当前价格,你可以添加下面给出的价格 $total_price+=$price;
回答by Robert
You need simple modification in your payableAmount() function
您需要对payableAmount() 函数进行简单的修改
function payableAmount(&$totalPrice){
// calculate total price here
$totalPrice += $totalPrice;
}
Variable is passed now via reference so it will be modified after you do it you can echo $totalPrice and it will be changed, the other 2 options is to use static variable or global. However if I were you I wouldn't use function for such a simply task. Just declare $total
before loop and inside loop $total += $price;
and it is enough
变量现在通过引用传递,因此在您执行此操作后将对其进行修改,您可以回显 $totalPrice 并且它将被更改,其他 2 个选项是使用静态变量或全局变量。但是,如果我是你,我不会将函数用于如此简单的任务。只需$total
在循环前和循环内声明$total += $price;
就足够了
回答by Chemaclass
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";
}