【问题标题】:Why grand total not not working in php code?为什么总计不能在 php 代码中工作?
【发布时间】:2020-07-14 21:23:37
【问题描述】:

当我生成总金额时,它只显示最后一行小计。我该代码中有一些错误。总金额未显示。 总计显示总是错误。 总金额没有显示在那里。 请帮帮我。 当添加这么多服务时,它有不同的不同价格。在这种情况下,总金额没有显示在那里。只显示最后一次服务费用。我还在下面添加了屏幕短。检查它以了解更多详细信息。

<table class="table table-bordered" width="100%" border="1"> 
    <tr>
        <th colspan="3">Services Details</th>   
    </tr>
    <tr>
        <th>#</th>  
        <th>Service</th>
        <th>Cost</th>
    </tr>

    <?php
    $ret = "select tblservices.ServiceName, tblservices.ServicePrice  
        from  tblinvoice 
        join tblservices on tblservices.ID=tblinvoice.ServiceId 
        where tblinvoice.BillingId=:invid";
    $query1 = $dbh -> prepare($ret);
    $query1->bindParam(':invid',$invid,PDO::PARAM_STR);
    $query1->execute();

    $results = $query1->fetchAll(PDO::FETCH_OBJ);

    $cnt = 1;
    if($query1->rowCount() > 0)
    {
        foreach($results as $row1)
        {               ?>

            <tr>
            <th><?php echo $cnt;?></th>
            <td><?php echo $row1->ServiceName?></td>    
            <td><?php echo "₹".$subtotal=$row1->ServicePrice?></td>
            </tr>

        <?php $cnt = $cnt+1;
        }
        $gtotal += $subtotal;
    } ?>

    <tr>
        <th colspan="2" style="text-align:center">Grand Total</th>
        <th><?php echo "₹".$gtotal?></th>   
    </tr>
</table>

【问题讨论】:

  • 你的 $gtotal 在你的 foreach 循环之外,所以它只得到最后一个值。将它向上移动一行,在大括号内,它会起作用。
  • $gtotal += $subtotal; 放入foreach 中。如果之后使用它,$gtotal 将只包含最后一个 $subtotal 的值。

标签: php html html-table


【解决方案1】:

$gtotal += $subtotal; 应该在 foreach 循环中,以便在我们迭代时对其求和。

<?php
    $ret = "select tblservices.ServiceName, tblservices.ServicePrice  
        from  tblinvoice 
        join tblservices on tblservices.ID=tblinvoice.ServiceId 
        where tblinvoice.BillingId=:invid";
    $query1 = $dbh -> prepare($ret);
    $query1->bindParam(':invid',$invid,PDO::PARAM_STR);
    $query1->execute();

    $results = $query1->fetchAll(PDO::FETCH_OBJ);

    $cnt = 1;
    if($query1->rowCount() > 0)
    {
        foreach($results as $row1)
        {               ?>

            <tr>
            <th><?php echo $cnt;?></th>
            <td><?php echo $row1->ServiceName?></td>    
            <td><?php echo "₹".$subtotal=$row1->ServicePrice?></td>
            </tr>

        <?php $cnt = $cnt+1;
              $gtotal += $subtotal;
        }
       
    } ?>

【讨论】:

  • 客户详细信息和发票 ID 显示两次....为什么?
  • AsiI 可以看到您在提供的 img 和代码中也没有 client detailsinvoice id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
相关资源
最近更新 更多