1

I'm using Perch, which is a php CMS. I'm asking this question here, as it seems like a general php question.

Basically, I'm using filter on a search page:

perch_content_custom('Jobs', array(
    'page'=>'/view-job.php',
    'template'=>'_job_list.html',
    'filter' => array(
        array(
            'filter'=>'job_subject',
            'match'=>'eq',
            'value'=> $_GET['subject']
            ),

        array(
            'filter'=>'job_term',
            'match'=>'eq',
            'value'=> $_GET['term']
        ),
    )
));

I only want to filter if the GET string has a value, - so if job_subject is specified, apply a filter - but I know I can't use an IF statement and I'm not sure of the alternative in php?

Urs
  • 4,984
  • 7
  • 54
  • 116
Dan Temple
  • 1,172
  • 2
  • 14
  • 22
  • instead of using `$_GET['subject']` which can have many different values threshold it down by using `((bool)$_GET['subject'])?"has_subject":"no_subject"` in the value assignment. – Orangepill May 16 '13 at 14:49

2 Answers2

0

What about this:

$filters = array();

if(!empty($_GET['subject'])) {
    $filters[] = array(
        'filter'=>'job_subject',
        'match'=>'eq',
        'value'=> $_GET['subject']
    );
}

if(!empty($_GET['term'])) {
    $filters[] = array(
        'filter'=>'job_term',
        'match'=>'eq',
        'value'=> $_GET['term']
    );
}

perch_content_custom('Jobs', array(
    'page'=>'/view-job.php',
    'template'=>'_job_list.html',
    'filter' => $filters
));

But I guess this won't be a good approach when there would be a filter with many entries... If the CMS does not support some better filter conditioning I guess it is not a very good solution...

shadyyx
  • 15,825
  • 6
  • 60
  • 95
0

Don't worry: Perch will match all values if the value field is empty, as if that filter would not exist.

Instead of an IF statement you can easily use the "ternary operator" (?:) here.

As an example, here's how you can avoid returning everything:

'value'=>(empty($_GET["s"]) ? 'undefined' : $_GET["s"])

Nick
  • 1