产品经理:
'21',
'or_get_reg_date' => '09',
'lt_reg_date' => '2012-12-19',
'in_id' => array(1, 2, 3),
);
$tmpConditions = transArrayTerms($postData);
echo $whereCause = getWhereSql($tmpConditions);
// WHERE managerid like '21%' OR reg_date'2012-12-19' AND id in ('1','2','3')
处理where条件的sql
&#39;&#39;<&#61;&#39;, &#39;gt&#39;&#61;>&#39;>&#39;, &#39;get&#39;&#61;>&#39;>&#61;&#39;, &#39;eq&#39;&#61;>&#39;&#61;&#39;, &#39;neq&#39;&#61;>&#39;<>&#39;);
foreach ($infoSearch as $term &#61;> $value) {
if (empty($value)) continue;
$name &#61; $term;
if (strpos($term, "or_") !&#61;&#61; false) { //添加or连接符
$terms[&#39;useOr&#39;] &#61; true;
$name &#61; str_replace("or_", "", $term);
}
if (strpos($name, "in_") !&#61;&#61; false) {
$terms[&#39;name&#39;] &#61; str_replace("in_", "", $name);
$terms[&#39;charCal&#39;] &#61; " in ";
$terms[&#39;value&#39;] &#61; "(&#39;" . implode("&#39;,&#39;", $value) . "&#39;)";
} else {
$terms[&#39;name&#39;] &#61; $name;
$terms[&#39;charCal&#39;] &#61; " like ";
$terms[&#39;value&#39;] &#61; "&#39;" . trim($value) . "%&#39;";
}
//放在else后面
foreach($separator as $charCalName &#61;>$charCalVal){
if (strpos($name, $charCalName."_") !&#61;&#61; false) {
$terms[&#39;name&#39;] &#61; str_replace($charCalName."_", "", $name);
$terms[&#39;charCal&#39;] &#61; $charCalVal;
$terms[&#39;value&#39;] &#61; "&#39;" . trim($value) . "&#39;";
}
}
$aryRst[] &#61; $terms;
unset($terms);
}
return $aryRst;
}
function whereOperator($has_where, $useOr) {
$operator &#61; $has_where ? ($useOr &#61;&#61;&#61; false ? &#39; AND &#39; : &#39; OR &#39;) : &#39; WHERE &#39;;
return $operator;
}
/**
* aryTerm transArrayTerms转化后的查询条件
* &#64;过滤没有输入的sql查询条件并转化成where条件.
*/
function getWhereSql($aryTerm) {
$whereCause &#61; &#39;&#39;;
if (count($aryTerm) > 0) {
$has_where &#61; &#39;&#39;;
foreach ($aryTerm as $value) {
$has_where &#61; whereOperator($has_where, isset($value[&#39;useOr&#39;]));
$whereCause .&#61; $has_where . $value[&#39;name&#39;] . $value[&#39;charCal&#39;] . $value[&#39;value&#39;];
}
}
return $whereCause;
}
本文原创发布php中文网&#xff0c;转载请注明出处&#xff0c;感谢您的尊重&#xff01;
相关文章
相关视频
网友评论
文明上网理性发言&#xff0c;请遵守 新闻评论服务协议我要评论