Help with a filtered list

I’m sure this has a really simple answer but being a relative novice with PHP I can’t figure it out, the following code enables the filtering by town for a series of data entries, the tags and results are all working but I need to have the default list off all entries before the tags are clicked. I’m sure it revolves around an ‘if’ and ‘else’ argument but I’m struggling. Any help would be greatly appreciated.

Thanks in advance.

Paul

<!-- LOCATION TAG CLOUD -->
<?php

    // fetch all locations
    $locationtags = $page->courses()->toStructure()->pluck('town', ',', true);

?>
                    
<ul class="tagcloud">
    <li><a href="<?php echo html($thispage) ?>">All locations</a>&nbsp;</li>
    <?php foreach($locationtags as $locationtag): ?>
    <li>
        <a href="<?php echo html($thispage . '/tag:' . $locationtag)?>"><?php echo html($locationtag) ?></a>&nbsp;
    </li>
    <?php endforeach ?>
</ul>
<!-- END LOCATION TAG CLOUD-->

<!-- LISTING BY LOCATION -->                   
<?php foreach($page->courses()->toStructure()->filterBy('town', param('tag'), ',') as $coursedetails): ?>
                    
<article>Content</article>

<?php endforeach ?>
<!-- END LISTING BY LOCATION -->

Exactly, what you need is an if-else statement:

<?php
if($tag = param('tag')) {
  $courses = $page->courses()->toStructure()->filterBy('town', $tag, ',');
} else {
   $courses = $page->courses()->toStructure();
}
foreach($courses as $course): ?>
//do stuff
<?php endforeach ?>

Awesome… you rock. I was over complicating things by trying to migrate/emulate code that worked in Kirby1, this is much lighter. Thanks again :slight_smile: