How to else from an foreach?

Hello everyone,

I’ve got following problem, I have a foreach loop that is working perfectly fine. But I’d like to display “No items available” if foreach finds nothing. How to achieve this with kirby?

currently I’ve got following code

<?php foreach($page->site()->index()->filterBy('documentindex', '*=', $code) as $item): ?>
<ul class="card-information">
    <li><h3><?= $item->title()->html() ?></h3></li>
    <li class="action"><?= $item->documentProtocollInformation()->kt()?></li>
</ul>
<?php endforeach ?>
???? <p>No items avaiable</p> ?????

You can’t else from a foreach, the syntax is if - else - endif

<?php 
$collection = $page->site()->index()->filterBy('documentindex', '*=', $code);
if($collection->count()):
  foreach( $collection as $item): ?>
  <ul class="card-information">
      <li><h3><?= $item->title()->html() ?></h3></li>
      <li class="action"><?= $item->documentProtocollInformation()->kt()?></li>
  </ul>
  <?php endforeach ?>
<?php else: ?>
<p>No items avaiable</p>
<?php endif ?>
1 Like