【问题标题】:Laravel 5 after 3 posts show an addLaravel 5 后 3 个帖子显示添加
【发布时间】:2023-03-25 10:52:01
【问题描述】:

大家好,我已经用变量 $lastItems 声明了我的帖子

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $item)
     @include('._posting.items')                                                  
  @endforeach                                    
@else
     @include('errors.emptycontent')
@endif

我想在每 3 个帖子后添加广告,我使用类似的东西

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $item)
     @include('._posting.items')                                                  
  @endforeach  
 @if($item = 3)
    <img src="link" alt="ads">
 @endif                                  
@else
     @include('errors.emptycontent')
@endif

但问题是,当我这样尝试时,它会在每个帖子之后显示广告,而不是在 3 个帖子之后。 我做错了什么?

【问题讨论】:

  • 我想你可能想仔细看看@if($item = 3)
  • 你的意思是“=”到“==”?

标签: php laravel-5 blade laravel-blade


【解决方案1】:

你可以试试:

@if($lastItems->total() > 0)                   
  @foreach($lastItems as $key => $item)

     @include('._posting.items')

     @if (($key + 1) % 3 == 0)
         <img src="link" alt="ads">
     @endif                                                  
  @endforeach                                 
@else
     @include('errors.emptycontent')
@endif

【讨论】:

  • 成功了,但在第 3 个帖子时它计数 +1,所以我在第 4 个帖子后收到它,之后可以吗?
  • 在这种情况下,如果 $key 从 0 开始,您将显示四个项目之后的第一个添加。
【解决方案2】:

我几乎遇到了同样的问题,在我的情况下,我不得不更改 div 的类。所以我用了这样的东西,它帮助了我:

    <?php $var = -1; ?>
@foreach($lastItems as $item)
  <?php $var ++;?>
    @if($var%3 == 0 && $var!=0)
       @include('errors.emptycontent')
    else
        @include('._posting.items')
   @endif
@endforeach

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2011-09-04
    • 2015-04-07
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多