这里我有一个foreach循环,可以查看客户端可查看的日期日历。在第一个循环中,我将$ i作为变量等于0.然后我给出规则,声明如果$ i等于0或者可以被3整除,则回显div类组。但是,如果循环在$ i等于1时退出,或者可以被2整除以关闭类组并继续向下移动html。出于某种原因,当我有一个或两个条目时,该组未正确关闭,并且内部捕获了以下div。请你看看这段代码,看看我搞砸了......
<div class = "calender_dates">
<div class = 'calender_select'>
<div class = 'dates past active'></div>
<div class = "dates future"></div>
</div>
<div class = "past_events grid-calender">
<?php $i=0 ?>
@foreach($past as $event)
<?php if($i==0 OR is_int($i/3)){
if($i==0){
echo '<div class="group_loop_first">';
} else{
echo '<div class="group_loop">';
}
} ?>
<div class = "p_date grid-date">
<div class = 'date_head'>
<div class = "head_img">
<p>img</p>
</div>
<div class = "head_description">
<p>{{$event->caption}}</p>
</div>
</div>
<div class = "date_foot">
<div class = "foot_date">
<p class = "day">
{{$event->publish_at->format('d')}}
</p>
<p class = "month">
{{$event->publish_at->format('M')}}
</p>
</div>
<div class = "foot_callout">
<p class = "name">{{$event->name}}</p>
<p>{{$event->address}}</p>
</div>
</div>
</div>
<?php $i++ ?>
<?php if($i==0 OR is_int($i/3)){
echo '</div>';
}?>
@endforeach
<?php
if($i==1 OR $i==2 OR is_int($i/2)){
echo '</div>';
}?>
</div>
<div class = "future_events grid-calender">
<?php $i=0 ?>
@foreach($future as $event)
<?php if($i==0 OR is_int($i/3)){
echo '<div class="group_loop">';
} ?>
<div class = "p_date grid-date">
<div class = 'date_head'>
<div class = "head_img">
<p>img</p>
</div>
<div class = "head_description">
<p>{{$event->caption}}</p>
</div>
</div>
<div class = "date_foot">
<div class = "foot_date">
<p class = "day">
{{$event->publish_at->format('d')}}
</p>
<p class = "month">
{{$event->publish_at->format('M')}}
</p>
</div>
<div class = "foot_callout">
<p class = "name">{{$event->name}}</p>
<p>{{$event->address}}</p>
</div>
</div>
</div>
<?php $i++ ?>
<?php if($i==0 OR is_int($i/3)){
echo '</div>';
}?>
@endforeach
<?php
if($i==1 OR is_int($i/2)){
echo '</div>';
}?>
</div>
答案 0 :(得分:1)
您遇到了问题,因为您过早地增加了计数器。下面的代码会被清理一下,但基本的想法是,在您检查是否需要结束div标签之前,不应该增加计数器。
<div class="past_events grid-calender">
<?php $i = 0; ?>
@foreach($past as $event)
@if ($i == 0)
<div class="group_loop_first">
@elseif ($i % 3 === 0)
<div class="group_loop">
@endif
<div class = "p_date grid-date">
<!-- rest of html -->
</div>
@if ($i % 3 === 0)
</div>
@endif
<?php $i++; ?>
@endforeach
</div>
<div class="future_events grid-calender">
<?php $i = 0; ?>
@foreach($future as $event)
@if ($i % 3 === 0)
<div class="group_loop">
@endif
<div class = "p_date grid-date">
<!-- rest of html -->
</div>
@if ($i % 3 === 0)
</div>
@endif
<?php $i++; ?>
@endforeach
</div>