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

我有一个具有唯一编号的 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']}} 并必须将其显示为总金额。


哆啦的时光机
浏览 95回答 1
1回答

ABOUTYOU

如果您只需要后面的值,foreach您可以使用一个单独的变量来添加金额:@php($amount = 0)@foreach ($results['polinfo'] as $data)&nbsp;&nbsp; &nbsp; @foreach ($data['poldata'] as $res)&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="row">{{$res['INSTALNO']}}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$res['PR_NO']}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$res['PR_DATE']}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{$res['AMOUNT']}}</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @php($amount += (int) $res['AMOUNT'])&nbsp; &nbsp; @endforeach@endforeachAmount: {{ $amount }}
打开App,查看更多内容
随时随地看视频慕课网APP