Jump to content

Filter logic


Sarnoc
 Share

Recommended Posts

Hi All,

Back again! Following on from my last post (useful for background), I'm now trying to build filters for my website to make it easier to find the correct page.

I've used the Skyscrapers style of building an array but I can't work out how to insert logic into the filter. For example, I have the following set up within my filters:

  1. Year_Sunday (1 of the 3 Sunday years)
  2. Year_Weekday (1 of the 2 weekday years)
  3. Season (1 of 6 or so seasons, e.g. Lent)
  4. Week (1, number of weeks dependent on length of season)

These are referencing page references, so each of those is a separate part of the site structure.

In my code, I've just got:

foreach(array('Liturgical_Year_Sunday', 'Liturgical_Year_Weekday', 'Liturgical_Season', 'Liturgical_Week') as $key) {
    if(!$value = $input->get($key)) continue;
}

as shown in the Skyscrapers demo.

This gives me the ability to apply filters, but the logic appears to be OR rather than AND?

For example, if I've got both Season and Week filters applied, (e.g. Lent, Week 4), I'll get any results that are tagged with Lent OR Week 4, when in fact what I want is the following filter logic:

Liturgical_Year_Sunday OR Liturgical_Year_Weekday AND/OR
Liturgical_Season AND Liturgical_Week

I'm guessing it's something to do with how you build the selector, but I can't see that in the Skyscrapers code. If someone could give me some hints on where to find it and how to implement this, it would be much appreciated.

Thanks

Link to comment
Share on other sites

On 5/12/2019 at 10:07 PM, Sarnoc said:

I'm guessing it's something to do with how you build the selector, but I can't see that in the Skyscrapers code.

Yes, it is about how you build the selector. When you are dealing with input coming from a search form you usually build up the selector in a string variable $selector, adding pieces to $selector for each populated GET variable.

But before you try that start with something simpler. Rather than building up a selector string dynamically, experiment with writing out different selector strings and supplying the selector to $pages->find() to see if you get back the pages you expect. The selectors documentation is here: https://processwire.com/docs/selectors/

Each "clause" you add to the selector works with AND logic by default:

field_1=foo, field_2=bar 

This matches pages where field_1=foo AND field_2=bar.

For OR logic with different fields and values, check the documentation for OR-groups.

(field_1=foo), (field_2=bar)

This matches pages where field_1=foo OR field_2=bar.

Regarding OR conditions for multiple values in a single field, or multiple fields with a single value, see the following sections:
https://processwire.com/docs/selectors/#or-selectors1
https://processwire.com/docs/selectors/#or-selectors2

When you have sorted out what kind of selector you need to match the pages you want you can move on to building up the selector dynamically according to your GET variables.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...