我有一個具有唯一編號的 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 }}
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消