2015-10-07 92 views
3

我使用我已存储在php数组中的产品ID从db获取产品的价格。如何在php中计算总价格

我有一个PHP函数,我想要保存所有购买的产品的总数。在这里,我正在购物车的物品,我打电话的功能,应该保持总量。每当我从购物车取回产品时,我都会传递值price

例如,对于第一个项目迭代我送价值300,和第二400,第三900

我想加起来所有这些,并获得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>&#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; 
} 
+1

为什么你想有一个函数来计算它可以同时内计算的东西吗? – Akshay

+0

'$ price = $ price + $ row ['product_price']'....如果你想要总价......把它放在一个循环中。 –

+0

为什么你没有使用while循环来计算总和 –

回答

3

您只需简单地更新你的查询,而不是像作为

$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']; 
} 
+0

但是它会对给定的product_id在循环中起作用吗?因为每次查询都会得到不同的product_id。所有的价格都要计算。 –

+0

然后它不会工作,那么你只需要简单的价格总和。我会更新它 –

+0

我尝试过,但我没有得到总计。而是我得到每个前例的价格:项目1 => 35000,项目2 => 16000总计应该是51000,但我越来越3500016000 –

2

只是外界宣布总 -

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

? :)除非使用全局变量或其他更聪明的解决方案,否则将无法工作我的朋友 – Robert

+0

感谢@罗伯特,很着急..现在修复了 –

0

创建函数外部的变量。然后,您将函数的价格添加到该变量中。并使用

$total += $totalprice. 
1

使用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>&#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(); 
} 
0

你可以简单地将它存储在变量添加在环路的价格,得到相同的。

payableAmount($ price); //不需要调用不同的函数,因为它只返回当前价格,而不是这个,您可以添加下面给出的价格 $ total_price + = $ price;

0

您需要在您的payableAmount简单的修改()函数

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

变量通过引用现已通过这样它会修改你这样做后,你可以回声$ totalPrice,它会被改变,另2选项是使用静态变量或全局。但是,如果我是你,我不会使用函数来完成这样简单的任务。循环之前和内环$total += $price;只是声明$total,它是足够

0
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"; 
    } 
+0

这会产生一个警告:undefined totalprice –

+0

好吧,尝试初始化var之前进入while循环:-) – Chemaclass