Forum

Thread tagged as: Question, Runway

Filtering collection by category using "and"

Hi,

I have a horrible feeling this might not be possible, I have a collection called jobs that I want to filter by category.

If I pass back an array of categories to filter by like so


// our object passed via AJAX if(isset($_GET['filters']) && !empty($_GET['filters'])){ $filters = $_GET['filters']; perch_collection('Jobs', array( 'category' => $filters['categories'], 'template' => '_job-list-item.html' , 'match'=>'and' )); }else{ perch_collection('Jobs', array( 'template' => '_job-list-item.html' )); }

This works in as far as it returns all items that have the assigned category, but it seems to be using an "or" where as I want and "and" as you would when using filtering on collections.

Hope this makes sense.

Many thanks

Lee

Lee Goodman

Lee Goodman 0 points

  • 5 years ago

OK so I just stumbled on another thread that mentioned the category-match option so my code looks like this


// our object passed via AJAX if(isset($_GET['filters']) && !empty($_GET['filters'])){ $filters = $_GET['filters']; print_r($filters); perch_collection('Jobs', array( 'category' => $filters['categories'], 'template' => '_job-list-item.html' , 'category-match' => 'all', )); }else{ perch_collection('Jobs', array( 'template' => '_job-list-item.html' )); }

but this isn't behaving as I would expect. Can anyone explain what this option is supposed to do, as I would expect the "all" to return items that match all categories? i.e catA and CatB

but if I print out my array being passed via ajax I get this

~~~

Array ( [categories] => Array ( [0] => job-types/permanent/ [1] => sectors/pharmaceutical/ ) )

~~

but in my return items I only get 1 job back with a job type of contract?

I've been at this all day so I may be losing the plot a bit but am I missing something?

Many thanks

Lee

hmm ok I think it may have been my $.ajax call caching!!!

OK - it's not caching, still a problem!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great!