簡體   English   中英

如何在php的while循環結果中添加值

[英]How to add values to in the result of while loop in php

如何添加存儲在變量 $profit 上的 while 循環的結果? 結果是 5 和 70

編碼

  <?php
        $sql2 = "SELECT * from `products`";
        $result2 = $link->query($sql2);

        while($row2 = $result2->fetch_assoc())
      {

      $sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
      $result3 = $link->query($sql3);
      $row3 = $result3->fetch_assoc();
      
      $sql = "SELECT  SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
      $result = $link->query($sql);
      $row = mysqli_fetch_array($result);

      $res = bcmul($row2['prod_price'], $row[0]);
      $profit = $res - $row2['prod_cost'];

      if($row[0] == null){
          $row[0] = 0;
      }
    }?>

試試這個代碼

  <?php
        $sql2 = "SELECT * from `products`";
        $result2 = $link->query($sql2);
        $totalProfit = 0;
        while($row2 = $result2->fetch_assoc())
      {

      $sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
      $result3 = $link->query($sql3);
      $row3 = $result3->fetch_assoc();
      
      $sql = "SELECT  SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
      $result = $link->query($sql);
      $row = mysqli_fetch_array($result);

      $res = bcmul($row2['prod_price'], $row[0]);
      $profit = $res - $row2['prod_cost'];
      $totalProfit = $totalProfit + $profit
      if($row[0] == null){
          $row[0] = 0;
      }
    }?>

現在您可以使用$totalProfit變量值作為總計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM