2017-07-20 2 views
1

foreachループを使用してブレードビューで印刷しようとしている配列データがあります。しかし、ここで私の条件は、バンドル名を印刷して、テーブルの中で1回だけ合計する必要があるということです。 配列データ:footerの値は、laravelの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

に以下のようなビューを取得していますが、私は結果ビューが好きなようにする必要があります必要がありますthis: enter image description here

ここでは合計を追加しませんbundle_nameのように表示されるはずです。 これを解決するのに役立つ人がいますか?ありがとうございました !

+0

は、なぜあなたは ''でコンテンツを始めていますか?それは ''でしょうか? –

+0

@Sagar Gautam:実際には、 '$ bundle_name!= false'の場合、' 'で始まっていません。 ''と終わりです。これを解決する他の方法があれば私を助けてください? – 06011991

+0

この行の '@if($ bundle_name!= false)'の後に ''の前に '<\tbody>'が再び現れます。それはタイプミスか何らかの理由でですか? –

答えて

1

、私は:)ここにあなたのソリューションをchagingていないよ、このソリューションをお試しください:

@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> 
+0

私はこれを試してみましょう – 06011991

+0

助けて幸いです) – Maraboc

+0

ねえ、それは完璧に働いてくれてありがとう:) – 06011991

関連する問題