I am writing Web services for IOS In cakePhp
and stuck in IN
condition.
I have a table dog_temperaments
and it has values "happy,dependent,shy"
.
Now if IOS send me array (happy,shy) Then I search Like this
Select dog_temperaments.* where temperaments IN(happy,shy)
Its work fine but if IOS send me array 0 or any(means search by any temperament) Then How I will Search........
Any means Search by all temperaments
Either you can check that the array is empty or not like
if(is_array($array)) {
Use
Select dog_temperaments.* where temperaments IN('happy','shy');
} else {
notify Enter proper search key
}
Else you anyways get the empty results by using the same query if the array is empty like
Select dog_temperaments.* where temperaments IN(0);
If it is 0
or any any
then no need for that condition as it ishould return all of them.
So aassuming $type
will contain the temperaments as an array and 0
/any
will be single element for that case.
if(count($type) == 1 && in_array(($type[0], array('0', 'any'))) {
$condition = "";
} else {
$condition = "WHERE temperaments IN ('" . implode("','", $type) . "')";
}
And the query will be like -
"Select dog_temperaments.* from dog_temperaments ".$condition