<?
function bq_where($where, $join = "and", $set = false, $level = 1){
if (!is_array($where)) return false;
$padding = str_repeat(" ", $level);
foreach ($where as $field => $value){
if (is_numeric($field)){
# It's a plain addition
$parts[] = $value;
} elseif (is_array($value)) {
# Subquery or IN-query
if (strstr($field, ":")){
[$one, $two] = explode(":", $field);
if (is_numeric($one)){
$field = $two;
} elseif (is_numeric($two)) {
$field = $one;
} else {
$field = $one;
$equals = $two;
}
}
if (in_array($field, ["and", "or"])){
$subjoin = $field;
$parts[] = "(" . bq_where($value, $subjoin, $set, $level + 1) . ")";
} elseif (in_array($equals, ["in", "not in"])) {
if (!is_array($value)) $value = [$value];
$parts[] = "$field $equals('" . join("', '", (array)$value) . "')";
}
} else {
$equals = "=";
if ($value == "NULL" && !$set) $equals = "is";
if (strstr($field, ":")) [$field, $equals] = explode(":", $field);
if (!is_int($value) && !preg_match("/[a-z_.]+(/i", $value) && !preg_match("/^[a-z_]{1,3}.[a-z_]+$/", $value) && $value != "NULL"){
$value = '"' . sqlesc($value) . '"';
}
$parts[] = "$field $equals $value";
}
}
$return = join(" $join ", $parts);
#$return = str_replace("nn", "n", $return);
#$return = str_replace("n", "n$padding", $return);
return $return;
}
?>
Note that the code is the exact code that I am using and may use functions that are exclusive to my system, and as such may not be suitable for copy/paste. Plus, these functions can change at any time and any blogs/tutorials that depend on these functioning in a specific way may have changed since it was written. If such a discrepancy is found, please email me at
If the function "lp()" is used, it's my "Language Print" function, that translates phrases from english to the site language. So, instead of "lp('and')" you would use just "and".
If the function "lp()" is used, it's my "Language Print" function, that translates phrases from english to the site language. So, instead of "lp('and')" you would use just "and".