【问题标题】:Laravel Blade - Header & Footer in a LoopLaravel Blade - 循环中的页眉和页脚
【发布时间】:2016-12-27 22:11:06
【问题描述】:

我有这样的数据按类别排序:

Category 1, Product 1, Amount
Category 1, Product 2, Amount
Category 2, Product 3, Amount 

我喜欢用刀片打印它

Category 1

Product 1    Amount
Product 2    Amount 
....
Total Amount of Category 1 : xxxxx


Category 2

Product 3    Amount 
....
Total Amount of Category 2 : xxxxx

【问题讨论】:

  • 我使用了 [@]foreach ($data as $row) [@]endforeach 我知道如何显示新类别的名称和总金额,主要问题是,我无法检查哪一行是任何特定类别的最后一个。
  • 为什么有人给这个问题-1?

标签: laravel eloquent blade


【解决方案1】:

我假设 $categories 是具有索引类别、产品和数量的排序多维数组。

   @for ($i = 0; $i < count($categories) ; $i++)
         <?php $category_product_toatl=0; ?>
         {{ $categories[$i]['category'] }}
         {{ $categories[$i]['product'] }}     {{ $categories[$i]['amount']}}
         <?php 
              $category_product_total= $category_product_total+ $categories[$i]['amount'];
         ?>
         @for ($j = $i+1; $j < count($categories) ; $j++)
              @if($categories[$i]['category']== $categories[$j]['category'])
                     {{ $categories[$j]['product'] }}     {{ $categories[$j]['amount']}}
                     <?php 
                          $category_product_total= $category_product_total+ $categories[$j]['amount']; 
                     ?>
               @else
                     break;
               @endif

        @endfor
   Total Amount of {{ $category[$i] }} : {{ $category_product_total }}
@endfor

【讨论】:

  • 感谢您的帮助。有没有办法找到任何类别的最后一行?
  • 是的,你写“break;”的地方@else 部分中的语句,它是任何类别的最后一行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多