Php 在@foreach循环中显示一次@section
嗨,我是拉威尔的新手,不能很好地理解Php 在@foreach循环中显示一次@section,php,laravel,foreach,sections,Php,Laravel,Foreach,Sections,嗨,我是拉威尔的新手,不能很好地理解部分,但我试图在循环中只显示一次特定的部分,然后重复所有其他内容,如果某个条件为真。这是我试图修改的代码。在这里,我评论了一行,我只想一次 @foreach ($pageTypes['news-events']->categories as $cat) <?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?>
部分,但我试图在循环中只显示一次特定的部分,然后重复所有其他内容,如果某个条件为真。这是我试图修改的代码。在这里,我评论了一行,我只想一次
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?>
@if ($related->count() > 0)
@section('tab-titles')
//i want to display this LI once if true
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach
@foreach($pageTypes['news-events']->类别为$cat)
@如果($related->count()>0)
@节(“选项卡标题”)
//如果这个LI是真的,我想显示一次
@附加
@节(“新闻内容”)
{{strotupper($cat->name)}
@foreach($news相关)
-
@endforeach
@附加
@恩迪夫
@endforeach
您应该使用else,因为您只想显示一次
请在此处查看文档
@if($related->count()>0)
@节(“新闻内容”)
{{strotupper($cat->name)}
@foreach($news相关)
-
@endforeach
@附加
@否则
@节(“选项卡标题”)
//如果这个LI是真的,我想显示一次
@附加
@恩迪夫
您可以添加一个简单的变量来检查某个节是否已显示在前面
$check = 0;
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?>
@if ($related->count() > 0)
if($check==0)
{
@section('tab-titles')
//i want to display this LI once if true
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
$check++;
}
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach
$check=0;
@foreach($pageTypes['news-events']->类别为$cat)
@如果($related->count()>0)
如果($check==0)
{
@节(“选项卡标题”)
//如果这个LI是真的,我想显示一次
@附加
$check++;
}
@节(“新闻内容”)
{{strotupper($cat->name)}
@foreach($news相关)
-
@endforeach
@附加
@恩迪夫
@endforeach
不知道这是不是最优雅的解决方案。但你可以通过以下方式实现你想要的:
<?php $repeat = true; // first initialize as true ?>
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id);
?>
@if ($related->count() > 0)
@if($your_certain_condition == true && $repeat)
<?php $repeat = false; ?>
@section('tab-titles')
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@endif
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach
@foreach($pageTypes['news-events']->类别为$cat)
@如果($related->count()>0)
@如果($your_sequence_condition==true&&$repeat)
@节(“选项卡标题”)
@附加
@恩迪夫
@节(“新闻内容”)
{{strotupper($cat->name)}
@foreach($news相关)
-
@endforeach
@附加
@恩迪夫
@endforeach
<?php $repeat = true; // first initialize as true ?>
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id);
?>
@if ($related->count() > 0)
@if($your_certain_condition == true && $repeat)
<?php $repeat = false; ?>
@section('tab-titles')
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@endif
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach