Show only "coming soon" events

Hello,

I’m searching to show events that don’t begin for now, and in a special section, I want to show just one of them. Then I did this :

<?php $allNow = $site->find('expositions','events')->children()->visible();
		$today = time();

		foreach($allNow->date(null, 'debut') > $today as $event): ?>

    <a href="<?= $event->url() ?>">
	    <ul>
			<li>Somehing</li>
	    </ul>
	    <figure>
	   		<img src="imgAdress">
	    </figure>
    </a>
<?php endforeach ?> 

It doesn’t run in this way, but the problem is that I wan’t just to show the first event of the foreach. I tried with an index before, but I have to put a if in the foreach to have just the events that doesn’t begin and my index is false.

<?php $allSoon = $site->find('expositions','events')->children()->visible();
		$today = time();

		$count = $allSoon->count();
		$index = 0;
		foreach($allSoon as $event): $index++;

	    $beginning = $event->date(null, 'beginning'); ?>
	   
<?php if($today < $beginning): ?>

    <a href="<?= $event->url() ?>">
	    <ul>
			<li>Something</li>
	    </ul>
	    <figure>
		    <img src="imgAdress">
	    </figure>
    </a>

<?php endif ?>
<?php endforeach ?>

Is it possible to filter the date in the foreach or before ? Or may I use a if ?

$allNow = $site->find('expositions','events')->children()->visible()->filter(function($child) {
  return $child->date(null, 'debut') > time();
});
// then loop over this to show all events

To get only the first event:

$firstEvent = $allNow->first(); // no loop, no if
echo $firstEvent->title();
1 Like

Thanks @texnixe !

Can I do that with current event ?
Something like return $child->date(null, 'begin') > time() && $child->date(null, 'end') < time() && $child->place() == "thisPlace"; ?

You can use multiple conditions in the filter callback (not on a single event, though, because filters only work with a collection), however, your logic doesn’t really make sense and will probably not return anything (with a start date in the future but an end date in the past)…

Yeah, I it’s a fault, sorry

I’m trying to compare $child with element in the page, but when I write something like $page->date() or $page->title() , $page seems to be undefined, I wrote it out of the function but same problem…

Please post your example code that doesn’t work as expected. If you want to use a variable you have defined outside the callback, you have to use use()
Example:


$country = 'whatever';
$allNow = $site->find('expositions','events')->children()->visible()->filter(function($child) use($country) {
  return $child->date(null, 'debut') > time();
});
<?php $first_name = $page->first_name()->value();
      $title = $page->title()->value();
      $same = $site->find('expositions','evenements')->children()->visible()->filter(function($event) {
            return $event->title()->value() == $first_name.' '.$title;
      });

or this

<?php $same = $site->find('expositions','evenements')->children()->visible()->filter(function($event) {
            return $event->title()->value() == $page->first_name()->value().' '.$page->title()->value();
      });

As I already said above, you have to use() the variables defined outside the callback:

<?php $first_name = $page->first_name()->value();
      $title = $page->title()->value();
      $same = $site->find('expositions','evenements')->children()->visible()->filter(function($event) use($title, $first_name){
            return $event->title()->value() == $first_name.' '.$title;
      });