I have an advanced search that searches real estate properties by the usual criteria: price range, Bedrooms, Bathrooms... In the form, the default is "Any" if they don't care about a particular option.

On the results page, I'm checking for a null value in case an option wasn't selected. Despite this, I'm still getting the message 'Key "1" for array with keys "0" does not exist' if the price option is missing. What is causing this?

My search results page looks like this:

{% set price = craft.request.getParam('p') %}
{% set price = price ? price|split('-') %}

{% set br = craft.request.getParam('br') %}
{% set br = br ? br %}

{% set ba = craft.request.getParam('ba') %}
{% set ba = ba ? ba %}

<h1>Search Results</h1>

{% set searchResults = craft.entries({
    section: 'properties',
    propertyPrice: price ? ['and', '>= ' ~ price[0],'<= ' ~ price[1]],
    bedrooms: br ? [ 'and', '>= ' ~ br ],
    bathrooms: ba ? [ 'and', '>= ' ~ ba ],
    order: 'score'
}) %}

{% if searchResults|length %}

    {% for entry in searchResults %}
    <li>{{ entry.title }}: {{ entry.propertyPrice|currency('USD', true) }}: BR: {{ entry.bedrooms }}, BA: {{ entry.bathrooms }}</li>
    {% endfor %}

{% else %}
<h1>No Results Found</h1>
{% endif %}

This error could happen if the p param is set to something, but the value doesn’t actually contain a hyphen.

If you want to find out what the problematic value is, enable Dev Mode and then check your browser console > Application Log > GET Info or POST Info (depending on the request type).

To make your code more bulletproof you can replace this line:

{% set price = price ? price|split('-') %}


{% set price = (price and price matches '/-/') ? price|split('-') %}
  • Thanks, that was indeed the problem. The price param is being set to 'search' in the GET array when no value is given. How does that happen? – Jonathan Melville Feb 20 '15 at 17:08
  • That would depend on your actual form. Can you post the rendered form HTML? – Brandon Kelly Feb 20 '15 at 17:11
  • My search form is here. The price param has the name 'p', which is getting set to 'search' if no option is selected. gist.github.com/jonathanmelville/3f6d57b20ad0ec9220c1 – Jonathan Melville Feb 20 '15 at 17:17
  • Is there any JavaScript code being executed on this form? Nothing in that HTML would result in a p=search param. And Craft isn’t doing anything that would manipulate the params like that either. – Brandon Kelly Feb 20 '15 at 17:22
  • 1
    Sorry, just realized what the problem is. p is the GET parameter that your .htaccess redirect is assigning the URI path to. You’ll have to choose a different name for that param (both in the search form and the results page), since it’s conflicting with that. – Brandon Kelly Feb 20 '15 at 17:25

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.