【问题标题】:Foreach Loop echo group errorForeach 循环回显组错误
【发布时间】: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标签内吗? &lt;?php @foreach($past as $event){ ?&gt;&lt;?php @endforeach ?&gt; 相同?
  • 抱歉我编辑评论太慢了!注意缺少{ };
  • 要检查$i是否能被2整除,使用$i % 2 == 0,类似地检查$i是否能被3整除,使用$i % 3 == 0
  • 同样&lt;?php @foreach($future as $event){ ?&gt; &lt;?php @endforeach;} ?&gt; 你真的不需要如此频繁地插入和退出php标签。您可以只保留一对代码块。
  • 这些似乎也在 php 之外以及{{$event-&gt;caption}} 和其他人。我本来希望&lt;?php echo $event-&gt;caption; ?&gt;

标签: php html laravel foreach


【解决方案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>

【讨论】:

  • 感谢清理!我不确定刀片模板的内容。
猜你喜欢
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 2019-01-14
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2014-06-06
相关资源
最近更新 更多