How to filter pages by arrays

#1

Hi to all, I have to two fields that have array of accepted and restricted countries for some review.
In the listings of all reviews I would like to create filter that will be search cross two this arrays.
First, we will check if the country is on the list of allowed countries and if it is not on the list of restricted countries. That is, the logic is that if it is not prohibited, it is allowed.
How best to do this?

$accepted_countries = array("United Kingdom", "Canada");
$restricted_countries = array("USA", "Belgium", "Netherlands");

I think it should works with

array_intersect

But how show it in controller, have no idea…

0 Likes

#2

Could you post your controller, please?

Can there be any overlap between these two arrays?

array_intersect($accepted_countries, $restrictedCountries) would give you all values of the first array that are also present in the second.

0 Likes

#3
<?php

return function($site, $pages, $page) {

  $perpage  = $page->perpage()->int();

  $all_reviews = $page->children()->listed()->sortBy('date')->flip();

  /* Accepted Countries */
  $countries = urldecode(param('countries'));  
 
  if ($countries) {
	  $query_countries = explode(',', get('countries'));
	  $exluded_countries = $reviews->search($query_country, 'excluded_countries');
  $allowed_countries = $reviews->search($query_country, 'allowed_countries');
  foreach ($query_countries as $country) {
		if (in_array($country, $allowed_countries) || !in_array($country, $exluded_countries)) {
		  return $countries;
		}
	}
  } 
  else {
$reviews = $page->children()->listed()->sortBy('date')->flip();
  }

  $reviews = $reviews->paginate($perpage);

  return [
'countries'    => $countries,    
'reviews'      => $reviews,
'all_reviews'  => $all_reviews,
'pagination'   => $reviews->pagination(), 
  ];

};

This is not final controller. In my mind it should work with different filters, in select options visitors could select one country or few countries. Maybe view of page will helps…)

0 Likes

#4

I don’t really understand your controller, because the variables used there go a bit wild/are not defined.

Could you also have a second look at my question above.

0 Likes

#5

Yep, ) im already see many mistakes. Ive took some parts from other controller that isn’t apply to this one.

I’m already understand that array_intersect will not helps me.
For example:

I have 3 reviews

Review 1
allowed country list: Austria, Canada, Finland
restricted country list: USA, Belgium

Review 2
allowed country list: Austria, Canada, New Zealand
restricted country list: USA, Germany

Review 3
allowed country list: Austria, Canada, New Zealand
restricted country list: Finland, USA, Belgium, Germany

User select in filter Finland, Belgium

The result shoud be
Review 1 and Review 2
Review 1 because it have Finland in allowed
Review 2 because it don’t have Finland and Belgium in restricted.

But) when I start write my answer, I’m just think that I can search only for countries that not in restricted_countries array) and don’t need allowed_countries at all… with this logic

0 Likes

#6

Exactly! You would only need the first if you want to restrict the filtered collection to reviews that are explicitly allowed. In that case, the second $restricted_countries array would be superfluous, though.

0 Likes