PHP多条件搜索
like模糊搜索 eq精准搜索(注意:like和eq是数组)
function csearch($like = 1, $eq = 1)
{
$where = [];
if ($like != 1) {
foreach ($like as $k => $v) {
if ($v != -1 && $v != "") {
$where[$k] = ['like', "%" . $v . "%"];
}
}
}
if ($eq != 1) {
foreach ($eq as $k => $v) {
if ($v != -1 && $v != "") {
$where[$k] = ['=', $v];
}
}
}
$str = "";
foreach ($where as $k => $v) {
$str .= $k . " " . $v[0] . " '" . $v[1] . "' and ";
}
$str = substr($str, 0, -4);
return $str;
}
有时间日期条件的数据处理
//对于时间范围的搜索处理 返回的是字符串
function searchtime($where, $time, $key)
{
$last_time = false;
$start = substr($time, 0, 10);
$start = strtotime($start);
$end = substr($time, -10);
$end = strtotime($end);
$end = $end-1;
if ($start !== false && $end !== false) {
$end = $end + 3600 * 24;
$last_time = $start . " and " . $end;
}
if ($where != false) {
if ($last_time != false) {
$where .= " and (" . $key . " between " . $last_time . ") ";
}
} else {
if ($last_time != false) {
$where .= $key . " between " . $last_time;
}
}
return $where;
}
转载地址:https://blog.csdn.net/phper23/article/details/115331545