php一般的多条件搜索功能
我这里是利用的laravel框架进行的操作
我们一般的查询 直接 like 就好
但是如果是多条件的话,会有个空值情况
这里讲解一下,空值的处理。
这里有五个条件(接受到的值)
$state = $request->post('state');
$meth = $request->post('meth');
$last_name = $request->post('last_name');
$next_name = $request->post('next_name');
$import = $request->post('import');
然后定义四个字符串(因为时间这里要取一个段时间)
$tj1 = "1=1";
$tj2 = "1=1";
$tj3 = "1=1";
$tj4 = "1=1";
进行判断 或者重新赋值
if(!empty($state))
{
$tj1 = "state = $state";
}
if(!empty($meth))
{
$tj2 = "meth = $meth";
}
if(!empty($last_name) && !empty($next_name))
{
$tj3 = "time between $last_name and $next_name";
}
if(!empty($import))
{
$tj4 = " import like '%$import% '";
}
进行查询 and连接就可以了,这样做的话,可以选择其中几个,而不会发生报错情况
DB::select("select * from shopps where $tj1 and $tj2 and $tj3 and $tj4")