Yii2 - 如何获得计算数字的总和

时间:2017-09-20 13:22:32

标签: php mysql view yii2

我正在处理yii2中的发票,其中存储了项priceqnty,但每个项目的amount都是按qnty和{{}}的乘积计算的{1}}。 我现在想要在发票下面得到发票的price,这个数字将是发票项目的所有totals的总和。请记住,amounts值是计算的而不是存储的,如何根据这些数字计算amount? 请注意,我无法直接从数据库表中获取值,因为totals值是其他函数的派生。

以下是我的观点:

price

1 个答案:

答案 0 :(得分:1)

创建变量

  1. $lineItemPrice:存储订单项价格
  2. $subTotal:存储所有行/行,即$lineItemPrice项目价格。
  3. 在foreach循环中添加$lineItemPrice中的所有$subTotal。并且,显示在您想要显示的末尾。要显示Total,您需要使用$subTotal计算tax并显示在Total列中。

    <强>代码

    <div class="row">
      <div class="col-xs-12 table-responsive">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>Item</th>
              <th>Description</th>
              <th>Qty</th>
              <th>Price</th>
              <th>Amount</th>
            </tr>
          </thead>
          <tbody>
            <?php 
            $subTotal = 0;
            foreach ($repeatInvoiceItems as $indexItem => $repeatInvoiceItem): ?>
              <tr>
                <td>
                  <?php echo $repeatInvoiceItem->item->item; ?>
                </td>
                <td>
                  <?php echo $repeatInvoiceItem->desc; ?>
                </td>
                <td>
                  <?php echo $repeatInvoiceItem->qnty; ?>
                </td>
                <td>
                  <?php echo $repeatInvoiceItem->itemPrice; ?>
                </td>
                <td>
                  <?php 
                  $lineItemPrice = $repeatInvoiceItem->qnty * $repeatInvoiceItem->itemPrice;
                  echo $lineItemPrice;
                  $subTotal += $lineItemPrice;
                  ?>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    
    <div class="row">
      <div class="col-xs-6">
        <p class="lead">Payment Instructions:</p>
        <p class="text-muted well well-sm no-shadow" style="margin-top: 10px;">
          <?php echo $model->payment_instructions; ?>
        </p>
      </div>
      <div class="col-xs-6">
        <div class="table-responsive">
          <table class="table">
            <tr>
              <th style="width:50%">Subtotal:</th>
              <td>$<?php echo number_format($subTotal,2);?></td>
            </tr>
            <tr>
              <th>Tax (9.3%)</th>
              <td>$10.34</td>
            </tr>
            <tr>
              <th>Total:</th>
              <td>$265.24</td>
            </tr>
          </table>
        </div>
      </div>
    </div>
    

    总计,

    <div class="col-xs-6">
      <div class="table-responsive">
        <table class="table">
          <tr>
            <th style="width:50%">Subtotal:</th>
            <td>$<?php echo number_format($subTotal,2);?></td>
          </tr>
          <?php
          $tax = 9.3;
          ?>
          <tr>
            <th>Tax (9.3%)</th>
            <td>
              <?php
              echo $taxAmount = ($tax * $subTotal)/100;
              ?>
            </td>
          </tr>
          <tr>
            <th>Total:</th>
            <td>
              <?php echo $grandTotal = $subTotal + $taxAmount;?>
            </td>
          </tr>
        </table>
      </div>
    </div>