通过where进行多条件查询的接口的制作时,可以设置一个$where变量,分别对获取到的不同变量进行模糊匹配。
$where['order_number'] = array('like', "%$order_number%"); $where['price'] = array('like', "%$price%"); $where['status'] = array('like', "%$status%"); $where['create_time'] = array('like', "%$create_time%");
之后进行sql语句查询,其中db()方法获取到数据表,where()方法中是$where变量数组,其中包含要查询的字段以及模糊匹配的值。
$result = db('recreation_order') ->where($where) ->select();
最后附上整体逻辑代码
public function index() { if ($this->request->isPost()) { $order_number = $this->request->param('order_number'); $price = $this->request->param('price'); $status = $this->request->param('status'); $create_time = $this->request->param('create_time'); $where['order_number'] = array('like', "%$order_number%"); $where['price'] = array('like', "%$price%"); $where['status'] = array('like', "%$status%"); $where['create_time'] = array('like', "%$create_time%"); $result = db('recreation_order') ->where($where) ->select(); if ($result) { $this->success('操作成功', $result); } else { $this->error('操作失败'); } } }