Here I have a foreach loop going through a calendar of dates viewable by the client. On the first loop i pass $i as a variable equal to 0. Then i give the rules stating that if $i is equal to 0 or is divisible by 3 then to echo the div class group. However if the loop exits when $i is equal to 1 or is divisible by 2 to close the class group and continue down the html. For some reason when I have 1 or two entries the group does not close correctly and the following div is captured inside. Will you please look at this code to see where im messing up...
<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>
You're running into issues because you're prematurely incrementing your counter. The code below is cleaned up a bit, but the basic idea is you shouldn't increment your counter until after you've checked if you need the closing div tag.
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.