简体   繁体   English

如何在php中计算总价格

[英]How to calculate total price in php

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. 我有一个PHP功能,在我想要持有所有购买的产品。 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. 我传递价值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? 我想把所有这些加起来,并在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>&#8377; </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;
}

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>&#8377; </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'];
}

Just declare total outside - 只需在外面宣布 -

 $total =0;
function payableAmount($totalPrice){    
   // calculate total price here
   global $total;
    $total += $totalPrice;
    echo $total;
}

Use the while loop to calculate the price. 使用while循环计算价格。 Declare a variable $totalPrice and then sum it within the while loop. 声明变量$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>&#8377; </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();
}

Create a Variable outside the function. 在函数外部创建变量。 Then you add the price to this variable in the funtion. 然后在功能中将价格添加到此变量中。 and use 并使用

$total += $totalprice.

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; paidAmount($ price); //不需要调用不同的函数,因为它只返回当前价格,而不是这个你可以添加价格,如$ total_price + = $ price;

You need simple modification in your payableAmount() function 您需要在paidAmount()函数中进行简单修改

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. 变量现在通过引用传递,因此它将在您执行后修改,您可以回显$ totalPrice并且它将被更改,其他2个选项是使用静态变量或全局。 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; 只需在循环和内部循环$total += $price;之前声明$total $total += $price; and it is enough 这就足够了

    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>&#8377; </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.

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