页脚值在laravel中的foreach内打印相同的数据

时间:2017-07-20 11:22:19

标签: php arrays laravel foreach

我有一个数组数据,我正在尝试使用foreach循环在我的刀片视图中打印。但在这里,我的条件是我需要打印捆绑名称,并在表格中只有一次。 数组数据:

$data =  Array
(
    [0] => Array
        (
            [id] => 10
            [asset_name] => Mini Paver
            [qty] => 2
            [days] => 5
            [bundle_name] => Bundle 1
            [total] => 1000
        )

    [1] => Array
        (
            [id] => 2
            [asset_name] => Roller
            [qty] => 2
            [days] => 5
            [bundle_name] => Bundle 1
            [total] => 1000
        )
    [2] => Array
        (
            [id] => 13
            [asset_name] => Medi Paver
            [qty] => 2
            [days] => 5
            [bundle_name] => Bundle 2
            [total] => 2000
        )

    [3] => Array
        (
            [id] => 15
            [asset_name] => Sweet Roller
            [qty] => 2
            [days] => 5
            [bundle_name] => Bundle 2
            [total] => 2000
        )
)

刀片视图代码:

@php ($bundle_name = false)
@foreach($data as $value) 
    @if($bundle_name != $value['bundle_name'])
        @if($bundle_name != false)
        </tbody>
        <tfoot>
        <tr>
        <th colspan="4"> <p> Total : {{ $value['total'] }} </p></th>
        </tr>
        </tfoot>
        </table> 
        @endif
    @php ($bundle_name = $value['bundle_name'])
    <table class="" style="width: 100%;border:1px solid #ccc">
        <thead>
            <tr>
                <th colspan="4"> <p> {{ $bundle_name }} </p></th>
            </tr>
            <tr>
                <th style="text-align: center">id</th>
                <th style="width:5%;text-align: center">Asset Category</th>
                <th style="width:5%;text-align: center">Days</th>
                <th style="width:5%;text-align: center">Qty</th>
            </tr>
        </thead>
        <tbody>
    @endif
            <tr>
                <th style="text-align: center">{{ $value['id'] }} </th>
                <th style="width:5%;text-align: center">{{ $value['asset_name'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['days'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['qty'] }}</th>
            </tr>
@endforeach
</tbody>
<tfoot>
<tr>
    <th colspan="4"> <p> Total : {{ $value['total'] }} </p></th>
</tr>
</tfoot>
</table> 

目前我正在获得如下图所示的视图 enter image description here

但是我需要结果视图应该是这样的: enter image description here

这里我不想添加总数,它应该像bundle_name一样显示。 有人可以帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个解决方案,我不会在这里找到你的解决方案:):

@php ($bundle_name = false)
@php ($total_value = 0)
@foreach($data as $value) 
    @if($bundle_name != $value['bundle_name'])
        @if($bundle_name != false)
            </tbody>
            <tfoot>
            <tr>
            <th colspan="4"> <p> Total : {{ $total_value }} </p></th>
            </tr>
            </tfoot>
            </table> 
        @endif
        @php ($bundle_name = $value['bundle_name'])
        <table class="" style="width: 100%;border:1px solid #ccc">
            <thead>
                <tr>
                    <th colspan="4"> <p> {{ $bundle_name }} </p></th>
                </tr>
                <tr>
                    <th style="text-align: center">id</th>
                    <th style="width:5%;text-align: center">Asset Category</th>
                    <th style="width:5%;text-align: center">Days</th>
                    <th style="width:5%;text-align: center">Qty</th>
                </tr>
            </thead>
            <tbody>
    @endif
        <tr>
            <th style="text-align: center">{{ $value['id'] }} </th>
            <th style="width:5%;text-align: center">{{ $value['asset_name'] }}</th>
            <th style="width:5%;text-align: center">{{ $value['days'] }}</th>
            <th style="width:5%;text-align: center">{{ $value['qty'] }}</th>
        </tr>
    @php ($total_value = $value['total'])
@endforeach
</tbody>
<tfoot>
<tr>
    <th colspan="4"> <p> Total : {{ $total_value }} </p></th>
</tr>
</tfoot>
</table>