【发布时间】:2016-03-02 03:05:53
【问题描述】:
在这里,我有一个 foreach 循环遍历客户端可查看的日期日历。在第一个循环中,我将 $i 作为等于 0 的变量传递。然后我给出规则,说明如果 $i 等于 0 或可被 3 整除,则回显 div 类组。但是,如果在 $i 等于 1 或可被 2 整除时退出循环以关闭类组并继续向下 html。出于某种原因,当我有 1 或 2 个条目时,该组无法正确关闭,并且在其中捕获了以下 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>
【问题讨论】:
-
这应该在php标签内吗?
<?php @foreach($past as $event){ ?>与<?php @endforeach ?>相同? -
抱歉我编辑评论太慢了!注意缺少
{ }和; -
要检查
$i是否能被2整除,使用$i % 2 == 0,类似地检查$i是否能被3整除,使用$i % 3 == 0。 -
同样
<?php @foreach($future as $event){ ?>和<?php @endforeach;} ?>你真的不需要如此频繁地插入和退出php标签。您可以只保留一对代码块。 -
这些似乎也在 php 之外以及
{{$event->caption}}和其他人。我本来希望<?php echo $event->caption; ?>