Ajax filter through JavaScript with AND, OR condition

i read this blog and developed ajax filter. but i am not able filter after mixing 2 conditions(i.e. i wand OR condition with AND condition)
Like someone click 3 check-boxes :- bs , cs , ec
then filter condition like this where bs = 1 and cs =1 or ec = 1
but my result only gives result of where bs = 1 and cs =1 and ec =1
please help me !

http://hibbard.eu/use-ajax-to-filter-mysql-results-set/

my file

<?php
$pdo = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password);

// $select = 'SELECT *';
$select = 'SELECT name, description';

// $select = 'SELECT id, name, image, description, city ';
$from = ' FROM university';
$where = ' WHERE TRUE';
$opts = isset($POST['filterOpts'])? $POST['filterOpts'] : array('');

if (in_array("bs", $opts)){
$where .= " AND bs = '1' ";
}

if (in_array("ms", $opts)){
$where .= " AND ms = '1' ";
}

if (in_array("phd", $opts)){
$where .= " AND phd = '1' ";
}

if (in_array("cs", $opts)){
$where .= " AND cs = 'yes' ";

}

if (in_array("mec", $opts)){
$where .= " AND mec = 'yes' ";
}

if (in_array("ec", $opts)){
$where .= " AND ec = 'yes' ";
}
if (in_array("management", $opts)){
$where .= " AND management = 'yes' ";

}

if (in_array("electrical", $opts)){
$where .= " AND electrical = 'yes' ";
}

if (in_array("civil", $opts)){
$where .= " AND civil = 'yes' ";
}

if (in_array("xyz", $opts)){
$where .= " AND city = 'xyz' ";

}

if (in_array("mno", $opts)){
$where .= " AND city = 'mno' ";
}

if (in_array("rst", $opts)){
$where .= " AND city = 'rst' ";
}

$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

i see no use of an or condition in your script. you also have to use appropriate brackets if you don’t want to rely on the default precedences: and > or. further you have type mismatches - you are speaking of x = 1 but the script says x = yes for the flag.

sorry for writing x = 1 in description, that is x = “yes”. means where cs=“yes”. please refer code
i am getting problem, when i am including or condition. i am not able to figure out how to include or condition
for the condition like - where bs=“1” and cs=“yes” or ec=“yes”
i.e. bs must be 1(true) and either cs or ec should be present or both should be present.
please help me in this condition.

you already postet where bs="1" and cs="yes" or ec="yes" thats nearly the SQL you are looking for.

yes. can you help me in code ? @James_Hibbard , @chorn
not getting result when tried these combination in code

if (in_array("bs", $opts)){
    $where .= " AND bs = '1' AND (cs='yes' OR ec='yes') ";
  }

i also tried this

if (in_array("bs", $opts)) {
	$where .= " AND bs = '1' ";
	if (in_array("cs", $opts)){
    $where .= " OR cs = 'yes' ";
	if (in_array("cs", $opts)){
    $where .= " OR es = 'yes' ";
  }
}

and this

 if (in_array_all("bs","cs","ec", $opts)){
   $where .= " AND bs = '1' AND (cs = 'yes' OR ec='yes') ";
 }

thanks in advance !

have a look at echo $sql and try it in phpmyadmin (or similar) to see if there are any matches, use $statement->errorInfo() to check on errors.

yes, i got the query

if(in_array("bs", $opts) && in_array("ec", $opts) && in_array("cs", $opts)){

    $where .= " AND (bs = '1' AND ( cs = 'yes' OR ec='yes' )) OR (bs='1' AND( ec='yes' OR cs='yes')) OR (bs='1' AND (ec='yes' AND cs='yes') )  ";

  }

but it is not efficient when we have 1000 of branches like cs,ec,mec,civil and we want or condition of branches with program like bs='1' and ec='yes' or cs='yes' or mec='yes'
please help me !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.