Parameter Based Searching/ Filtering

Hey there,

I’m fairly certain this has been covered before, I’ve found a few topics that are similar, so I do apologise in advance if this is a tired topic.
Anyway, the gist of what I need to accomplish is product filtering based on input fields (parameters basically). I’ve followed this: https://getkirby.com/docs/cookbook/filtering#using-filters-with-forms-e-g-select-fields
Which has helped me set up the form, and what I am assuming is the functionality - my issue comes from not knowing how to set up my template to be filtered, if that makes any sense.
This is more or less the layout that I am trying to achieve:

I’ve found this in another topic: http://smartphone-star.de/finder/
That is pretty much the functionality that I am looking for, if that helps any.

If anyone could offer any help that would be great, I’m fairly new to Kirby and php in general, so layman’s terms are greatly appreciated.

Thanks much.

In the example from the cookbook you quoted above, the filtering happens in the controller, not in the template. The template then only renders the results that are passed down from the controller via variables.

Ahhh, okay that makes sense. This may be blatantly obvious but how should go about getting those results from the controller? As in displaying it in the template. Sorry if I’ve asked the same question in two different ways now.

In addition, if you have any alternatives to that cookbook example that would be great, I quoted that one because it seemed to offer a fairly straightforward approach.

Let’s say you pass a variable called $products from the controller to your template, where $products is either a collection of all products or a collection of filtered products depending on whether or not a filter is set.

Then you can just loop through that collection in your template, e.g.

<?php
foreach($products as $product): ?>
  <h1><?= $product->title() ?></h1>
  <!-- some more stuff here like description and prices -->
<?php endforeach ?>
```
The specifics depend of course on the fields you use and the HTML you want to render in the page.

So, the logic is all in the controller, the template then only echoes the stuff that is dynamic in your HTML.

That worked! Sometimes the simplest things go over my head. Okay, one final question, would it be possible to have this work with multiple filters at once?
Currently my form is essentially what is in the cookbook example - I’m trying to paste it here, but the markdown is getting in the way, basically I’m just using the onchange=“this.form.submit()” on the select.
I’m figuring if I remove that and use a submit button this should work? Sorry if this isn’t completely clear - the bones of the question revolves around filtering by multiple filters.

Thanks again for your help so far! It’s gotten me much farther than I could have on my own.

The example does in fact filter by multiple criteria at once. With onchange... the form is only submitted with each change of choice, but all criteria are submitted and used in the filtering if several form fields are selected. You should see that reflected in the URL.

Okay perfect, I’m actually not seeing it reflected in the URL, just getting a page refresh and updated results, this isn’t too concerning though, as the form is functioning. Out of curiosity if I do want to convert the form to submitting all the selected filters at once, would the submit button solution be the way to go?

Yes, you can remove the JavaScript and instead use a submit button :slight_smile:.

You are fantastic! Thank you so much for your help!

You are welcome :slight_smile:.

A post was split to a new topic: How to create a range slider