2

Following on from my earlier search question...

Searching for entries across multiple fields including a relationship

I'm finding that the min/max wage search behaves oddly.

I have a range of wage values from 95 to 175.

If I search for a min of 95 with no max then I only get results from 95 up to 100.

Likewise if I search for a max value of 150 then I only get results greater than 100 and less then or equal to 150.

If I use both min and max criteria then I get no results.

I'm guessing this is something to do with variable type but can't see how I can force the variables to be integers/numbers rather than strings.

The simplified code I'm using to do the matching is:

{% set wage_from = craft.request.getParam('wage_from') ? craft.request.getParam('wage_from') : 0 %}
{% set wage_to = craft.request.getParam('wage_to') ? craft.request.getParam('wage_to') : 9999 %}

{%
  set results = craft.entries({
      section: 'vacancies',
        wage: [
          'and',
          '>= ' ~ wage_from,
          '<= ' ~ wage_to,
        ]
    })
%}
2

Aha! Another lightbulb moment.

The wage field was set to be plain text. I changed it to number and things work as expected.

2
  • Yup! That's because MySQL/PHP handle string comparisons of numbers different than integer comparisons of numbers. – Brad Bell Oct 12 '15 at 15:28
  • 1
    I figured that's what the problem was. I was trying to change the type in my template rather than just change the field type. This is only my 2nd Craft project and the other was a simple prototype. I'm still learning! :) – foamcow Oct 12 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.