Error implementing "OR" in search of datatable

0

I'm doing a dating record system where I use datatables to show appointments that have a specific status. Everything went well until I filtered the datatable to show me only citations that are of one type or another (if they are appointments with status 'waiting for confirmation' or 'reprogrammed')

In order to show the data, I do it in the following way:

$sWhere = " WHERE  c.idEstatus = 'En espera de Confirmación' OR c.idEstatus = 'Reprogramada'";
    if ( $_GET['sSearch'] != "" )
    {
        $sWhere = "WHERE (";
        for ( $i=0 ; $i<count($aColumnas) ; $i++ )
        {
            $sWhere .= $aColumnas[$i]." LIKE '%".$_GET['sSearch']."%' OR ";
        }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ')';
        $sWhere .= "AND c.idEstatus = 'En espera de Confirmación' OR c.idEstatus = 'Reprogramada'";
    }

with the $ sWhere I restrict what will be visible, until there is all right, but at the moment where I say

$sWhere .= "AND c.idEstatus = 'En espera de Confirmación' OR c.idEstatus = 'Reprogramada'";

is happening to me that when writing "reprogrammed" it looks for all the reprogrammed ones, but when writing "awaiting confirmation" it looks for those of that type and at the same time it looks for those that are also reprogrammed and should not. This only happens when I do the search and I do not understand why. What could be happening? I do not know if the problem is the use of the OR.

Apart from the above, I also have:

// Filtrado de columna individual 
    for ( $i=0 ; $i<count($aColumnas) ; $i++ )
    {
        if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= $aColumnas[$i]." LIKE '%".$_GET['sSearch_'.$i]."%'";
        }
    }


    //Obtener datos para mostrar SQL queries

    $sQuery = "
    SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumnas))."
    FROM citas c 
    INNER JOIN personas p ON p.cedula = c.personas_cedula 
    $sWhere
    $sOrder
    $sLimit
    ";

    $rResult = $mysqli->query($sQuery);
    
asked by rodrigo2324 12.08.2017 в 02:47
source

1 answer

1

everything is fine but what you need is to be very clear as it works the ORs and the ANDs I recommend that you group the ORs between () so that ALL that set is True or False if any or none of the conditional, as you do with the FOR

$sWhere .= "AND (c.idEstatus = 'En espera de Confirmación' OR c.idEstatus = 'Reprogramada')";

This way, you are forcing the First to evaluate the OR and then to carry out the validation with the AND

    
answered by 12.08.2017 / 06:08
source