Multiple if elseif Combination needed for generating query string

I was checking on google for this solution but did not get a mature one.

Actually I’m setting up filters by getting the parameters from URL. I Have 4 Variable in URL and maximum combination i needed was around 28.

=====================================================

This is my url string (just break the variables part)

product_cat=booking&min_prep_time=19&max_prep_time=22

What Challenge I Am Facing
i am using if elseif for making combination but this slow down the performance and also code length is also increasing. i know about the switch and ternary operator i dont think they reduce the code length short and also performance.

The Example code is only with 2 variables & When my condition is true i am making array this array containing different values on the base of unique if elseif true.

if ( !isset($_GET['product_cat']) && isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {

         #$tax_query[] = array( putting values in array );
    }
    elseif ( !isset($_GET['product_cat']) && !isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {
      #$tax_query[] = array( putting values in array );
     }
    elseif ( isset($_GET['product_cat']) && $_GET['product_cat']=='group-buy' && isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {

        #$tax_query[] = array( putting values in array );
    }
    elseif ( isset($_GET['product_cat']) && $_GET['product_cat']=='group-buy' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {

        #$tax_query[] = array( putting values in array );
    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']=='booking' && isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        #$tax_query[] = array( putting values in array );
    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']=='booking' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        #$tax_query[] = array( putting values in array );
    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']!='booking' &&  $_GET['product_cat']!='group-buy' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        #$tax_query[] = array( putting values in array );

    }

What i Want

When i make this if elseif for 28 combination for getting query string then the code length is increased. i want code is well optimized and clean that would be easier for reading and modification in future too.

1 Like

You could make the code shorter just by assigning those tests to variables instead of calling isset() time and time again. I’m sure you could arrange them in a more logical fashion instead of just calling if - elseif over and over. For example your first variable, product_cat, is either set or not set, so you could have a nested if for that:


if (isset($_GET['product_cat'])) {
  $c = $_GET['product_cat'];
  switch ($c) { 
    case "group-buy": 
      // some stuff
      break;
    case "booking":
      // some stuff
      break;
    }
}
else {
  // all the options if product_cat isn_t set.

}

If any of the combinations of being set or not set are invalid, you could weed those out first and send the user back to make a proper selection. If there are no invalid selections, then maybe if the user doesn’t make a choice you have some default values, so you could define those prior to finally deciding how to proceed.

2 Likes

How will $tax_query change, depending on the input variables?

1 Like

Hi @hmusman and a warm welcome to the forum.

Try setting the following defaults which should make the if statements a lot easier by testing for TRUE:

<?php

$prod = isset($_GET['product_cat'] ? $_GET['product_cat'] : FALSE;
$max = isset($_GET['max_prep_time'] ? $_GET['max_prep_time'] : FALSE;
$min = isset($_GET['min_prep_time'] ? $_GET['min_prep_time'] : FALSE;

if ($prod && $max && $min)
{
//
}else if(...) {
// 
}else if(...) {
//
}else if(...) {
//
else {
//
}

2 Likes

Version: 002

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

$prod = isset($_GET['product_cat'])    ? $_GET['product_cat']     : FALSE;
$max  = isset($_GET['max_prep_time'])  ? $_GET['max_prep_time']  : FALSE;
$min  = isset($_GET['min_prep_time'])  ? $_GET['min_prep_time']  : FALSE;

echo '<pre>$_GET ==> ' .print_r($_GET, true) .'</pre>';
$links = [
    '',
    'nothing-set',
    'min_prep_time=12&max_prep_time=22',
    'product_cat=group_buy&max_prep_time=12',
    'product_cat=booking&max_prep_time=12&min_prep_time=32',
    'product_cat=booking&max_prep_time=12',
    'max_prep_time=12',
    'not-used',
    ];
foreach( $links as $id => $link):
    echo '<br><a href="?' .$link .'">' .$link .'</a>';
endforeach;
echo '<hr>';

$msg = 'DEFAULT';
// if ( !isset($_GET['product_cat']) && isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {
if( ! $prod && $max && $min )
{
  $msg = '! $prod && $max && $min ==> #$tax_query[] = array( putting values in array)';

}elseif ( ! $prod && !$min && $max ){
  $msg = '! $prod && !$min && $max ==> #$tax_query[] = array( putting values in array)';
 
}elseif ( $prod && ($prod =='group-buy') && $min &&  $max ) { 
  $msg = '$prod && $prod =="group-buy" && $min &&  $max ==> #$tax_query[] = array( putting values in array)';

} elseif ( $prod && $prod=="group-buy" && !$min &&  $max ) {
  $msg = '$prod && $prod=="group-buy" && !$min &&  $max ==> #$tax_query[] = array( putting values in array)';

} elseif( $prod && ($prod=="booking") && $min && $max) {
  $msg = '$prod && $prod=="booking" && $min , $max ==> #$tax_query[] = array( putting values in array)';

} elseif( $prod && $prod=='booking' && !$min && $max) {
  $msg = '$prod && $prod=="booking" && !$min && $max ==> #$tax_query[] = array( putting values in array)';

} elseif( $prod && $prod !='booking' &&  $prod !='group-buy' && !$min && $max){
  $msg = '$prod && $prod !="booking" &&  $prod !="group-buy" && !$min && $max ==>  #$tax_query[] = array( putting values in array)';
} else {
  $msg = 'Should never get here';
}

echo '<h3 style="color:red">' .$msg .'</h3>';

Output:

1 Like

If I understand the result you are looking for, this should work for you. Just add your additional parameters to the $allowed array. Notice that I added a random not allowed key/value to the GET array that is not processed as well as an empty allowed Key.

<?php declare(strict_types = 1);

$allowed = [
      'product_cat'
    , 'min_prep_time'
    , 'max_prep_time'
];

$_GET = ['randomval' => 'SomethingElse', 'product_cat' => 'booking', 'min_prep_time' => '', 'max_prep_time' => 22];

foreach ($_GET as $k => $v) {

    if (in_array($k, $allowed) && !(empty($v))) {
        $tax_query[$k] = $v;
    }
}

echo '<pre>', print_r($tax_query, true), '</pre>';
1 Like

yes its depending upon what i get from url

this is a great solution but why you are using link array what is the logic behind that ?

Your original post stated there are 28 different combinations and it is a lot easier for each combination to be single item in the array. I have only shown about six tests and the other tests can easily be added.

like if i use array for 28 different combination then i dont need to use if elseif anymore ??
can you please put small example for clear understand about the array logic that you shared.

Did you try running the example and adding extra conditions?

The else ifs are still required and if the Get statement is not evaluated a default message will be displayed. An if else condition will then be necessary to trap the extra condition.

I am unable to post another example because I am using a tablet and not able to valid the test script example.

yes i tried but i love if we handle this with loop. yes i tried with if else its work good but i just to see it work with loop logic that you shared.

Take a look at @droopsnoot’s post #2 for further optimisation. The switch statement for the “product_cat” groups different options and makes extra options very easy to add.

ok noted i will check and share my experience here.

What are the chances that you will need to modify the conditions? Would you benefit from a system that allows non-programmer management to update it? If so then you should consider an expert system and/or a knowledge base (Artificial Intelligence). As best as I understand what a knowledge base is, they are a database of conditions, such as a database of “if” conditions. An expert system is software that processes knowledge bases. There is a book about AI in the C language that might help, you would not need the complexity of most expert systems and/or knowledge bases.

However Microsoft offers AI in Azure and IBM offers AI in Bluemix (Bluemix is now IBM Cloud). I think Watson is an IBM expert system.

Did you run what I posted? Does it work for you or not? If not, why not?