我想计算 foreach 循环中的 if 语句 laravel 5.6
I want to count in if statement in foreach loop laravel 5.6
因为我是 Laravel 的新手,所以遇到这个问题并尝试了其他方法,但它不起作用。
这是我的 blade 文件
products.blade.php
@foreach($products as $product)
<div class="women">
<h6><a href="route{{'product.single}}">{{$product->title}}</a></h6>
<span class="size">XL / XXL / S </span>
<p ><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
@endforeach
为什么这不起作用
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
或者如何在迭代中对产品进行计数并在 if 语句中比较计数?
如果您的产品多于或少于 3 个,您的 if 语句将永远不会显示。您要查找的是产品数组中的第三项,您可以这样做:
@foreach($products as $key => $product)
<div class="women">
<h6><a href="route{{'product.single}}">{{$product->title}}</a></h6>
<span class="size">XL / XXL / S </span>
<p><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
@if($key == 2)
<div class="clearfix"></div>
@endif
@endforeach
要获取循环的索引,请使用
@foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
@endforeach
将if
条件添加到key
告诉我进展如何
无论在循环内部还是外部,数组计数始终相同。
如果您想根据当前迭代做出决定,例如。 "on every third product put a clearfix div",如果键是数字,则将条件应用于键。
Blade 提供了一个 loop
变量和一个 iteration
属性(从 1 开始)来帮助解决这个问题,参见这里 https://laravel.com/docs/5.6/blade#the-loop-variable
每三个产品的示例:
@foreach($products as $product)
...
@if ($loop->$loop->iteration%3 == 0)
<div class="clearfix"></div>
@endif
...
@endforeach
仅第三个产品的示例:
@if ($loop->$loop->iteration == 3)
<div class="clearfix"></div>
@endif
你可以这样使用loop variable。
而不是:
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
你应该使用:
@if($loop->iteration == 3)
<div class="clearfix"></div>
@endif
但是您很有可能在每 3 个元素(3、6、9 等)之后需要它,因此更好的解决方案可能是:
@if($loop->iteration % 3 == 0)
<div class="clearfix"></div>
@endif
您的示例无效,因为 $product 只是一个对象,因此 count($product)
不会有预期值。此外,如果您使用 count($products)
(注意尾随 s),它将不起作用,因为每个循环迭代中的产品数量相同。
因为我是 Laravel 的新手,所以遇到这个问题并尝试了其他方法,但它不起作用。 这是我的 blade 文件
products.blade.php
@foreach($products as $product)
<div class="women">
<h6><a href="route{{'product.single}}">{{$product->title}}</a></h6>
<span class="size">XL / XXL / S </span>
<p ><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
@endforeach
为什么这不起作用
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
或者如何在迭代中对产品进行计数并在 if 语句中比较计数?
如果您的产品多于或少于 3 个,您的 if 语句将永远不会显示。您要查找的是产品数组中的第三项,您可以这样做:
@foreach($products as $key => $product)
<div class="women">
<h6><a href="route{{'product.single}}">{{$product->title}}</a></h6>
<span class="size">XL / XXL / S </span>
<p><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
@if($key == 2)
<div class="clearfix"></div>
@endif
@endforeach
要获取循环的索引,请使用
@foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
@endforeach
将if
条件添加到key
告诉我进展如何
无论在循环内部还是外部,数组计数始终相同。
如果您想根据当前迭代做出决定,例如。 "on every third product put a clearfix div",如果键是数字,则将条件应用于键。
Blade 提供了一个 loop
变量和一个 iteration
属性(从 1 开始)来帮助解决这个问题,参见这里 https://laravel.com/docs/5.6/blade#the-loop-variable
每三个产品的示例:
@foreach($products as $product)
...
@if ($loop->$loop->iteration%3 == 0)
<div class="clearfix"></div>
@endif
...
@endforeach
仅第三个产品的示例:
@if ($loop->$loop->iteration == 3)
<div class="clearfix"></div>
@endif
你可以这样使用loop variable。
而不是:
@if(count($product) == 3)
<div class="clearfix"></div>
@endif
你应该使用:
@if($loop->iteration == 3)
<div class="clearfix"></div>
@endif
但是您很有可能在每 3 个元素(3、6、9 等)之后需要它,因此更好的解决方案可能是:
@if($loop->iteration % 3 == 0)
<div class="clearfix"></div>
@endif
您的示例无效,因为 $product 只是一个对象,因此 count($product)
不会有预期值。此外,如果您使用 count($products)
(注意尾随 s),它将不起作用,因为每个循环迭代中的产品数量相同。