【问题标题】:How to calculate total price in php如何在php中计算总价
【发布时间】:2015-10-07 07:47:33
【问题描述】:

我使用存储在 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>&#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;
}

【问题讨论】:

  • 你为什么要一个函数来计算可以在while内计算的东西?
  • $price = $price + $row['product_price'].... 如果你想要总价.... 把它放在一个循环中。
  • 为什么不使用while循环来计算总和
  • 你可以在你的 SQL 语句中使用 sum(price)

标签: php mysql


【解决方案1】:

你只是简单地更新你的查询,而不是像

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

【讨论】:

  • 但它是否适用于循环中给定的 product_id?因为查询每次都会得到不同的product_id。所有的价格都必须计算出来。
  • 那么它就行不通了,那么你只需要简单地需要价格的总和。我会更新的
  • 我试过了,但我没有得到全部。相反,我得到每个 ex 的价格:项目 1 => 35000,项目 2 => 16000 总计应该是 51000,但我得到 3500016000
  • 你在$row['product_price']中得到的价值是多少
  • 3500016000 是我在 $row['product_price']; 内得到的值;
【解决方案2】:

只需在外面声明总数-

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

【讨论】:

  • 在 PHP 中? :) 除非您使用全局变量或任何其他更聪明的解决方案,否则我的朋友将无法使用
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    在函数外创建一个变量。然后将价格添加到函数中的此变量中。并使用

    $total += $totalprice.
    

    【讨论】:

      【解决方案5】:

      您可以简单地在循环中添加价格并通过将其存储在变量中来获得相同的价格。

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

      【讨论】:

        【解决方案6】:

        您需要对您的 paidAmount() 函数进行简单修改

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

        变量现在是通过引用传递的,所以它会在你这样做后被修改,你可以回显 $totalPrice 并且它会被改变,另外两个选项是使用静态变量或全局变量。但是,如果我是你,我不会将函数用于如此简单的任务。只需在循环前声明$total,循环内声明$total += $price;就足够了

        【讨论】:

          【解决方案7】:
              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";
              }   
          

          【讨论】:

          • 这会产生一个警告:undefined totalprice
          • 好的,在进入while循环之前尝试初始化var :-)
          猜你喜欢
          • 1970-01-01
          • 2018-02-07
          • 1970-01-01
          • 2020-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多