dps43378 2017-03-21 15:32 采纳率: 0%
浏览 13

检查结果是否超过30天,并在上面添加标题文本

I got a grid with Facebook posts. I want to make an archive that people can open when clicking on a button. When clicked the facebook posts older than 30 days are shown, this needs to be done with ajax but that is for another time. First I need to know how I can seperate the data in my foreach loop.

When the loop reaches the first post that is 30 days or older I want a header text above it showing: 'archive'.

So like this:

post
post
post
post

archive
post(30days or older)
post(30days or older)
post(30days or older)
post(30days or older)

How can I do this? I already know how old posts are, see my code:

foreach ( $feedarray->data as $key => $feed_data )
  {
    if($feed_data->full_picture != ''){
      $fbimage = $feed_data->full_picture;
    }else{
      $fbimage = 'images/placeholder/placeholder.png';
    }

    $shortstrfb = substr($feed_data->message, 0, 70) . '...';
    if($feed_data->message != ''){
      $f++;
    }


    $date1 = date("y-m-d");
    $date2 = date("y-m-d",strtotime($feed_data->updated_time));


    $datetime1 = date_create($date1);
    $datetime2 = date_create($date2);
    $interval = date_diff($datetime1, $datetime2);
    $result = $interval->format('%R%a days');



    // if($f > 5){
    //   break;
    // }

    if($feed_data->message != '' && $feed_data->from->name == 'FB page name'){

      if($result > -30){
        $dagen = 'more than 30';
      }else{
        $dagen = 'less than 30';
      }

      $facebookfeed .= '
          <div class="col-md-4 col-sm-6 col-xs-12 item">
              <div class="vk-project vk-project-grid-item">
                  <div class="vk-img-frame">
                  <span class="datecorner">'.date("d-m-Y",strtotime($feed_data->updated_time)).''.$dagen.'</span>
                      <a href="'.$feed_data->permalink_url.'" class="vk-img">
                          <img style="width:370px;height:270px;object-fit:cover;" src="'.$fbimage.'" alt="">
                      </a>
                  </div>
                  <div class="content-hidden">
                      <p class="inbeeldtext"><a href="'.$feed_data->permalink_url.'">'.$shortstrfb.'</a></p>
                  </div>
              </div>
          </div>';
    }
  }
  echo $facebookfeed;

How can this be done?

  • 写回答

1条回答 默认 最新

  • douqian1517 2017-03-21 15:44
    关注

    You can place a variable outside the loop and set it to true once the time limit is reached.

    By checking against the state of this variable, you can prevent the 'archive' headline from appearing more than once.

    $reachedArchive = false;
    
    foreach(...) {
    
        $postUpdated = strtotime($feed_data->updated_time);
    
        if (!$reachedArchive && $postUpdated <= strtotime('-30 days')) {
            $reachedArchive = true;
            echo '<h2>Archive</h2>';
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)