mercredi 3 juillet 2019

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

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

        echo "not set prod cat 1st ifelse, but set min and max prep time";

    }
    elseif ( !isset($_GET['product_cat']) && !isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {
      echo "not set prod cat 2nd ifelse, and also not set min and max prep time";

     }
    elseif ( isset($_GET['product_cat']) && $_GET['product_cat']=='group-buy' && isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {

        echo "set prod cat = group-buy 3rd ifelse, and set min and max prep time";

    }
    elseif ( isset($_GET['product_cat']) && $_GET['product_cat']=='group-buy' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time']) ) {

        echo "set prod cat = group-buy 4th ifelse, but not set min and max prep time";

    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']=='booking' && isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        echo "set prod cat = booking 5th ifelse,  and also set min and max prep time";

    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']=='booking' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        echo "set prod cat = booking 6th ifelse,  but not set min and max prep time";

    }
    elseif( isset($_GET['product_cat']) && $_GET['product_cat']!='booking' &&  $_GET['product_cat']!='group-buy' && !isset($_GET['min_prep_time'] , $_GET['max_prep_time'])){

        echo " prod cat != booking and prod cat != group-buy  7th ifelse,  but not set min and max prep time";

    }

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.

Aucun commentaire:

Enregistrer un commentaire