I have 5 variables, I need to generate a SQL code

0

- Hi, it's my first posting, I hope you can understand me clearly. -

I have 5 variables which are of the Bool type. True or False.

I need to be able to generate a SQL statement depends on the states of the variables.

I was able to generate it in a very extensive way, I wanted to know if there is a function that facilitates the code.

Ex code:

$sql = "SELECT * FROM bitacora WHERE ";

if(($VL_errores == "true") and ($VL_ingresos == "false") and ($VL_config == "false") and ($VL_pesaje == "false") and ($VL_motores == "false")){
    $sql .= "codigo LIKE '0%'";
}

if(($VL_errores == "true") and ($VL_ingresos == "true") and ($VL_config == "false") and ($VL_pesaje == "false") and ($VL_motores == "false")){
    $sql .= "codigo LIKE '0%' or codigo LIKE '1%'";
}

if(($VL_errores == "true") and ($VL_ingresos == "true") and ($VL_config == "true") and ($VL_pesaje == "false") and ($VL_motores == "false")){
    $sql .= "codigo LIKE '0%' or codigo LIKE '1%' or codigo LIKE '2%'";
}

The issue is that I have to generate 32 lines of codes for all possibilities.

Thank you very much!

    
asked by Gabriel Bellome 19.10.2018 в 16:02
source

2 answers

0

If the variables are BOOLEANAS .. because compare them with true or false ???

$sql = "SELECT * FROM bitacora WHERE ";

if (($ VL_errors) and (NOT $ VL_incomes) and (not $ VL_config) and (not $ VL_traffic) and (not $ VL_motors)) {     $ sql.="LIKE code '0%'"; }

if (($ VL_errors) and ($ VL_incomes) and (not $ VL_config) and (not $ VL_pear) and (not $ VL_motors)) {     $ sql.="LIKE code '0%' or code LIKE '1%'"; }

if (($ VL_errors) and ($ VL_incomes) and ($ VL_config) and (not $ VL_traffic) and (not $ VL_motors)) {     $ sql.="LIKE code '0%' or code LIKE '1%' or code LIKE '2%'"; }

    
answered by 21.10.2018 / 00:59
source
1

I could solve it, I'll leave the sample here:

$sql = "SELECT * FROM bitacora";

if(($VL_errores == "true") || ($VL_ingresos == "true") || ($VL_config == "true") || ($VL_motores == "true") || ($VL_pesaje == "true")){
    $sql .= " WHERE ";
}

if($VL_errores == "true"){
    $sql .= "codigo LIKE '0%'";
}
if($VL_ingresos == "true"){
    if($VL_errores == "true"){
        $sql .= " or ";
    }
        $sql .= "codigo LIKE '1%'";
}
if($VL_config == "true"){
    if(($VL_errores == "true") || ($VL_ingresos == "true")){
        $sql .= " or ";
    }
        $sql .= "codigo LIKE '2%'";
}
if($VL_motores == "true"){
    if(($VL_errores == "true") || ($VL_ingresos == "true") || ($VL_config == "true")){
        $sql .= " or ";
    }
        $sql .= "codigo LIKE '3%'";
}
if($VL_pesaje == "true"){
    if(($VL_errores == "true") || ($VL_ingresos == "true") || ($VL_config == "true") || ($VL_motores == "true")){
        $sql .= " or ";
    }
        $sql .= "codigo LIKE '4%'";
}  
    
answered by 19.10.2018 в 16:23