[英]<UL> <LI> Foreach Loop Displaying all content on one <UL>
I know this is relatively simple but it's driving me absolutely insane! 我知道这是相对简单的,但它使我发疯!
a little background, my app takes an XML feed to parse, this has nested fields such as features 有一点背景,我的应用程序需要一个XML feed来解析,其中包含诸如功能之类的嵌套字段
it's in my DB, as an obj/array, with 'feature' as the key. 它以obj / array的形式存在于我的数据库中,以“功能”为键。
now my for each loop is displaying all this data in one UL. 现在,我的每个循环都在一个UL中显示所有这些数据。
Code Below. 下面的代码。
Any Help Greatly Appreciated 任何帮助,感激不尽
//model
public static function getFeatures($id){
$featuresArray = [];
$featuresquery = DB::table('properties')
->selectRaw("features")
->where('id', $id)
->get('');
$k = $featuresquery;
foreach ($k as $feat):
if (is_array($feat)) {
$featuresArray[] = (array_key_exists('features', $feat)) ? $feat['features'] : '';
}
return $feat;
endforeach;
return $featuresArray;
}
//Controller
public function details($id) {
$property = Property::findorFail($id);
$agent = Agent::where("id",$property->agent_id)
->with([
'ads'
])->first();
$template = Agent::getTemplateName($agent);
$banners = Banner::getBanners($agent->id);
$areaAndLocations = Property::getAreaAndLocations($agent->id);
$property_type = Property::getPropertyTypes($agent->id);
$query_params = Property::getQueryParams();
$features = Property::getFeatures($property->id);
return view('frontend.'.$template.'.property.details', ['template' => $template, 'banners' => $banners, 'areaAndLocations' => $areaAndLocations, 'property_type' => $property_type, 'query_params' => $query_params, 'features' => $features])
->withDetails($property)
->withAgent($agent);
}
//View
<div class="features">
@foreach($features as $feature)
<ul><li>
{!! json_encode(json_decode($feature)) !!}
</li></ul>
@endforeach
</div>
because you giving ul>li
in foreach which generating ul
with every iterate, follow the below code 因为您在foreach中给了
ul>li
,每次迭代都会生成ul
,所以请遵循以下代码
<div class="features">
<ul>
@foreach($features as $feature)
<li>
{!! json_encode(json_decode($feature)) !!}
</li>
@endforeach
</ul>
</div>
Move the <ul>
and </ul>
tags to outside the for loop. 将
<ul>
和</ul>
标记移至for循环之外。 Your code is generating a new <ul>...</ul>
pair for every item on the list. 您的代码正在为列表中的每个项目生成一个新的
<ul>...</ul>
对。
Change the foreach loop like this 像这样更改foreach循环
<div class="features">
<ul>
@foreach($features as $feature)
<li>
{!! json_encode(json_decode($feature)) !!}
</li>
@endforeach
</ul>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.