We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Adding filters with GET variables

Hi all ,

Im trying to perform a search by using the filter options with collections. I'm facing a bit of trouble with actually making the filter part dynamic . have a look at what im trying to achieve .

    $collection = perch_collection('property', [
    'template'=>'search_result.html',
   'filter' => [
       foreach($_GET as $key => $value){
          [
            'filter' => $key,
            'match'  => 'eq',
            'value'  => $value,
        ], 
       }
    ]
    ]
,true);  

How do i make the filters add to the array ? Plus i have also tried one more thing .

Passing a range to the filter variable . My output from GET is "0-300000000"

$exploded=explode("-",$pricerangetemp);
$value = print_r ("'".$exploded[0] . "," . $exploded[1] . "'"); 

  [
           'filter' => 'price',
            'match'  => 'eqbetween',
            'value'  =>$value,
        ],

This doesnt seem to work .

Titus Saju

Titus Saju 0 points

  • 5 months ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

This is just a PHP syntax point, really. To translate what you're doing directly, I would do this:

$filters = [];
foreach($_GET as $key => $value){ 
    $filters[] = [ 'filter' => $key, 'match' => 'eq', 'value' => $value, ];
} 

$collection = perch_collection('property', [ 
    'template'=>'search_result.html', 
    'filter' => $filters,
] ,true); 

But you should be careful doing that, as it would break if anything unexpected was on the query string. That happens more often than you might think.

So instead, check against known good values ($permitted_keys below):

$permitted_keys = ['price', 'brand', 'size'];
$filters = [];
foreach($_GET as $key => $value){ 
    if (in_array($key, $permitted_keys)) {
        $filters[] = [ 'filter' => $key, 'match' => 'eq', 'value' => $value, ]; 
    }
} 

$collection = perch_collection('property', [ 
    'template'=>'search_result.html', 
    'filter' => $filters,
] ,true); 

Hi Drew ,

Thanks a lot for your reply !

Btw , how do i work the second part out .

Passing a range to the filter variable . My output from GET is "0-300000000"

$exploded=explode("-",$pricerangetemp);
$value = print_r ("'".$exploded[0] . "," . $exploded[1] . "'"); 

  [
           'filter' => 'price',
            'match'  => 'eqbetween',
            'value'  =>$value,
        ],

This doesnt seem to work .

Got this resolved .

Drew McLellan

Drew McLellan 2638 points
Perch Support

$value = implode(',', $exploded);

Hi Drew ,

i just displayed the filters array . Do let me know what is wrong here .

Array
(
    [0] => Array
        (
            [filter] => offering_type
            [match] => eq
            [value] => Residential property for rent
        )

    [1] => Array
        (
            [filter] => price
            [match] => eqbetween
            [value] => 0,3000000000000000
        )

    [2] => Array
        (
            [filter] => sub-location
            [match] => eq
            [value] => DIFC
        )

)

I am using the same for loop with a basic add on .

foreach($_GET as $key => $value){ 
    if (in_array($key, $permitted_keys) && $key != "price") {
        $filters[] = [ 'filter' => $key, 'match' => 'eq', 'value' => $value, ]; 
    }
    if($key == "price" ){
        $filters[] = [ 'filter' => $key, 'match' => 'eqbetween', 'value' => $value, ]; 
    }
} 
Drew McLellan

Drew McLellan 2638 points
Perch Support

What problem are you seeing?