Forum

Thread tagged as: Forms

User filterable list

Hi,

I've got a user filterable list set up to filter jobs based on text found in the job title and a location <select>.

In the location <select> there are several locations - North West, South East, etc. There's also an All Areas option which when selected should just display all the jobs.

I can't figure out the best way of doing this? Obviously 'All areas' isn't a value in a field and as such isn't marked against any jobs.

Any help or pointers would be really helpful.

Many thanks,

John.

John Robinson

John Robinson 7 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'd make the option empty or a known value like 'all'. If that value is set, don't apply the filter.

Thanks Drew,

Can I be cheeky and ask for some code pointers?

This what I've got so far, I'm mindful that I can't completely turn the filter off because the text search still needs to work on 'all' locations.

Hope that makes sense and thanks.


<?php // Create an empty array ready to add our filters to $filters = array(); if (perch_get('textsearch')) { // if 'type' is on the URL, add a filter for bedrooms $filters[] = array( 'filter' => 'title', 'match' => 'contains', 'value' => perch_get('textsearch'), ); } if (perch_get('location')) { // if 'beds' is on the URL, add a filter for bedrooms $filters[] = array( 'filter' => 'location', 'match' => 'eq', 'value' => perch_get('location'), ); } // Unset the filters if none are used: if (!count($filters)) $filters=false; // Then get the list perch_content_custom('Jobs', array( 'template' => 'job-listing.html', 'sort' => 'published', 'sort-order' => 'DESC', 'filter' => $filters, )); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd just change:

if (perch_get('location')) {

to something like

if (perch_get('location') && perch_get{'location')!='all') {

Works a treat!

Many, many thanks Drew.

Sorry to bother you again Drew, just one other small thing.

If I enter say a partial word into the text field, like "grad" instead of "graduate" it doesn't return a result, even though it's set up with 'contains'.

Is this normal behaviour or have I done something wrong?

Drew McLellan

Drew McLellan 2638 points
Perch Support

A 'contains' filter matches against word boundaries. It won't find a partial word.

Ok, thanks Drew.

Hi Drew,

Is it possible to set one filter against two fields so that one text input could search both?

Something like this perhaps:

'filter' => 'title,description';

I've tried putting them in as separate filters but they seem to cancel each other out.

Drew McLellan

Drew McLellan 2638 points
Perch Support

See the section "Filtering by multiple fields" here: https://docs.grabaperch.com/docs/content/perch-content-custom/

Thanks