Query Conditions for two Filter Fields

I have two fields, item_search and points_search. I’m trying to filter a table by either field if only one is filled in, or both if they are both filled in. I can’t seem to get the logic right in the Conditions. The item search is filtered by ‘contains’ and the points_search by ‘less or equal’ if that matters.

Thx.

What exactly is the problem?

The contains is easy as if empty it returns all records as it resolves to a wildcard

The less than or equal needs to be set to only be enforced if and entry exists

You can do this by attaching a condition to the filter like below (may have to change the condition slightly to suit your defaults)

In the above case the condition is enforced if $_GET.filterval >0 (sorry should have used less than or equal to match your question)

Well, I’ve tried every iteration I can think of and none work. For instance, I would have thought this would work…

but that will filter either just the item_name or item_name AND item_points, but not just points. Flipping them around it works the other way. OR doesn’t work as it shows items that are either in item_search or <= to point_search.

What I want is if I fill in just the item_search it will filter by item_search, if I fill in just point_search it will filter by just that and if both are filled in it will filter by both.

Thanks.

Have you tried using the “Add Group” feature? Group your two conditions into two separate groups. That may work.

In your search does the user have to enter a value in both fields? If not, change your ‘AND’ to ‘OR’?

Yes. Those are two groups. I’ve tried a whole bunch of other ways as well and nothing seems to work.

This with any combination of AND/OR doesn’t work

Nor does this

Or any of the other ways I’ve tried.

I was hoping someone would have done it before and have a definitive answer.

In these examples, you’re nesting the conditions. If I understand correctly, you need something like this, eg:

Notice that each condition is at the same level (not nested).

1 Like

Ah ha!!! Thanks Tom! I knew there was some bit I was missing and that was it. Didn’t tweak to me to not put the first condition in that first box, but to add the group first. Makes sense now. Your example didn’t quite work, but this did…

I’m glad you got it working, though I would have thought you’ve made it more complicated than necessary.

That’s what my example does. Eg using the countries in my example, the result will be ‘UK’, whether I search for:
country = UK (input1)
code = GBR (input2)
country = UK | code = GBR (both inputs used)

1 Like

I swear I tried it like that and it didn’t work! Went back to try it again and voila! Maybe I didn’t save the server action properly or something.

Anyway, it’s working like…

Thanks for your time, and patience!

1 Like