多条件搜索方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值