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?
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>';
}
}