为了账号安全,请及时绑定邮箱和手机立即绑定

从来自 api 的表列中获取总金额的总和

从来自 api 的表列中获取总金额的总和

PHP
哆啦的时光机 2023-07-08 21:42:35
我有一个具有唯一编号的 api,该编号不同,因此当我使用该唯一编号调用 api 时,我会返回数据,并在表中显示数据。该表有一个列名称“amount”。由于其动态数据来自 API,我只是困惑如何在表格的最底部显示总量。我正在使用 Laravel。我的代码示例<table class="table table-one">       <thead class="table-success">            <tr>                <th scope="col">Inst. No.</th>                <th scope="col">PR No.</th>                <th scope="col">PR Date</th>                 <th scope="col">Prem. Amt.</th>            </tr>        </thead><!-- ends: thead -->        <tbody>         @foreach ($results['polinfo'] as $data)           @foreach ($data['poldata'] as $res)            <tr>                <th scope="row">{{$res['INSTALNO']}}</th>                <td>{{$res['PR_NO']}}</td>                <td>{{$res['PR_DATE']}}</td>                <td>{{$res['AMOUNT']}}</td>            </tr>       @endforeach         @endforeach        </tbody><!-- ends: tbody -->    </table>我必须计算所有 {{$res['AMOUNT']}} 并必须将其显示为总金额。
查看完整描述

1 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

如果您只需要后面的值,foreach您可以使用一个单独的变量来添加金额:


@php($amount = 0)


@foreach ($results['polinfo'] as $data) 

    @foreach ($data['poldata'] as $res)

        <tr>

            <th scope="row">{{$res['INSTALNO']}}</th>

            <td>{{$res['PR_NO']}}</td>

            <td>{{$res['PR_DATE']}}</td>

            <td>{{$res['AMOUNT']}}</td>

        </tr>

        

        @php($amount += (int) $res['AMOUNT'])

    @endforeach

@endforeach


Amount: {{ $amount }}


查看完整回答
反对 回复 2023-07-08
  • 1 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信