如何解决如何在laravel刀片表中循环数据?
我在laravel刀片文件中有此表。
@foreach($items as $item)
<table class="pack-table">
<tr>
<th width="30%">Color</th>
<th width="10%">Pack</th>
<th width="60%">Total Units</th>
</tr>
<tr>
<td>
@foreach($item['grid'] as $color => $sizes)
{{$color}}<br><br>
@endforeach
</td>
<td>
@foreach ($item['grid'] as $color => $sizes)
@foreach ($sizes as $size)
{{ $size['description'] }}<br><br>
@endforeach
@endforeach
</td>
<td>
@foreach ($item['grid'] as $color => $sizes)
@foreach ($sizes as $size)
{{ $size['total'] }}<br><br>
@endforeach
@endforeach
</td>
</tr>
</table>
@endforeach
此代码将打印一张表格,如下图所示。
这仅打印每个数据。但我希望将其打印如下: 例如,颜色 VELBL 的尺寸为 MEDIUM,X LARGE,LARGE,X SMALL和SMALL ,所有这些包装尺寸的总单位为 0
因此,我需要先打印所有这些记录,然后再打印下一个颜色 BLUAS ,它是包装的大小,以及每个的总和。
如何修改我的代码以根据需要打印?
<table class="pack-table">
<tr>
<th width="30%">Color</th>
<th width="10%">Pack</th>
<th width="60%">Total Units</th>
</tr>
@foreach($item['grid'] as $color => $sizes)
<tr>
<td colspan="3" align="left">{{ $color }}</td>
</tr>
@foreach ($sizes as $size)
<tr>
<td>{{ $size['description'] }}</td>
<td>{{ $size['total'] }}</td>
</tr>
@endforeach
@endforeach
</table>
解决方法
这就是您想要的循环更少的
@foreach($items as $item)
<table class="pack-table">
<tr>
<th width="30%">Color</th>
<th width="10%">Pack</th>
<th width="60%">Total Units</th>
</tr>
@foreach($item['grid'] as $color => $sizes)
<tr>
<td colspan="{{count($sizes)+1}}">
{{$color}}
</td>
<?php $totals = 0 ?>
@foreach ($sizes as $size)
<td>
{{ $size['description'] }}
</td>
<td>
<?php $totals += $size['total'] ?>
{{ $size['total'] }}
</td>
</tr>
<tr>
@endforeach
<td>TOTAL</td>
<td>{{$totals}}</td>
</tr>
@endforeach
</table>
@endforeach
,
使用此代码
// don't use props.promotion,use props.promo instead
<RenderCard item={props.promo} />
,
我认为是这样的:
@foreach($items as $item)
<table class="pack-table">
<tr>
<th width="30%">Color</th>
<th width="10%">Pack</th>
<th width="60%">Total Units</th>
</tr>
@foreach($item['grid'] as $color => $sizes)
<tr>
<td>{{ $color }}</td>
<td>
<table>
<?php $total = 0; ?>
@foreach ($sizes as $size)
<tr>
<td>{{ $size['description'] }}</td>
<tr>
<?php $total += $size['total'] ?>
@endforeach
</table>
</td>
<td>{{ $total }}</td>
</tr>
@endforeach
</table>
@endforeach
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。