我在<section>
中有一个循环:
<section>
<h4 role="heading" aria-level="4">@lang('site.scope')</h4>
<ul>
@for( $i = 1; $i <= 4; $i++)
@if( $card->{"gen_champ__q" . $i } == 1 )
<li>@lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
@endif
@endfor
</ul>
</section>
但如果(== 1)我没有符合条件的数据。 所以,我想隐藏完整的部分。
但我不知道怎么做,你有什么想法吗?如果我超出我的部分,它将复制我的内容,而这不是我想要的。
答案 0 :(得分:1)
您将使用第一个&lt;部分来编写该部分的“标题”。李&GT; (并且只有第一个)你必须写。并且只有在任何&lt;部分中,您才会写出该部分的“页脚”。李&GT;写了。
@for( $i = 1; $i <= 4; $i++)
@if( $card->{"gen_champ__q" . $i } == 1 )
@if (!isset($doSection) && $doSection=true)
<section>
<h4 role="heading" aria-level="4">@lang('site.scope')</h4>
<ul>
@endif
<li>@lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
@endif
@endfor
@if (isset($doSection))
</ul>
</section>
@endif
编辑:刚升级以避免不必要的变量初始化。
答案 1 :(得分:-1)
@for( $i = 1; $i <= 4; $i++)
@if( ! empty( $card->{"gen_champ__q" . $i })
&& $card->{"gen_champ__q" . $i } == 1 )
<li>@lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
@endif
@endfor
答案 2 :(得分:-1)
如果在开始之前你可以加一个。
@if($i>0)
<section>
<h4 role="heading" aria-level="4">@lang('site.scope')</h4>
<ul>
@for( $i = 1; $i <= 4; $i++)
@if( $card->{"gen_champ__q" . $i } == 1 )
<li>@lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
@endif
@endfor
</ul>
</section>
@endif
答案 3 :(得分:-1)
有很多方法可以做到这一点就是这个。 在这种情况下,当你的情况满足时,它会打印出来而不是
@for( $i = 1,$j=1; $i <= 4; $i++)
@if( $card->{"gen_champ__q" . $i } == 1 )
@if($j==1&&$j--)
<section>
<h4 role="heading" aria-level="4">@lang('site.scope')</h4>
<ul>
@endif
<li>@lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
@endif
@endfor
@if($j==0)
</ul>
</section>
@endif
希望这会对你有帮助!