【发布时间】:2018-02-05 09:04:12
【问题描述】:
这是我将数据存储到数组中的代码,但它不起作用,页面只显示一个元素,这是数组中的最后一个元素。请帮忙。
$modProduct = array();
if($product->product->subcategory->category->id==1){
$modProduct[1] = $product->product;
}
if($product->product->subcategory->category->id==4){
$modProduct[4] = $product->product;
}
if($product->product->subcategory->category->id==7){
$modProduct[7] = $product->product;
}
if($product->product->subcategory->category->id==10){
$modProduct[10] = $product->product;
}
if($product->product->subcategory->category->id==13){
$modProduct[13] = $product->product;
}
if($product->product->subcategory->category->id==16){
$modProduct[16] = $product->product;
}
这是我的输出:
@foreach($modProduct as $index => $modProducts)
<div ><a href="#tab-{{ $index }}" class="tab-nav" data-toggle="tab">
{{$modProducts->subcategory->category->name}}</a></div>
@endforeach
【问题讨论】:
-
你预计有多少,6?那么如果满足条件,你确定另一个?
-
我在数据库中的数据有 6 个类别,这就是为什么我做一个 if 语句来验证它们。
-
在你的控制器中返回你在 foreach 循环之外的值,因为它返回你最后一个值