tp5.1小程序搜索接口

本文详细介绍了一种基于ThinkPHP框架的后端数据查询方法,通过接收前端传递的参数,如姓名、时间范围、合同编号等,构建多条件查询语句,实现数据的精确筛选与返回。文章提供了具体代码实例,展示了如何使用where条件数组和Db类进行复杂查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,第一步获取前端页面传过来的数据
2,编写where条件数组

$input=input(“name”);

2,编写where条件数组 编写的时候注意数组格式 ,这里就直接上图了

在这里插入图片描述
数组条件可以替换成自己需要的条件,我这里用的都是模糊查询
3, 使用 多条件whereOr 进行查询

$ list=Db::table(‘company_account’)->whereOr($where) ->select();

4,获取数据查询长度

$ count=count($list);

5,返回数据

return json([‘code’=>200,‘data’ => $list,‘count’ => $count]);

上原图
在这里插入图片描述

后续修改 为了应对不同方式的搜索

 public function getByInputSelectAccount(){
        $name=Request::instance()->post('name'); //姓名
        $startTime=strtotime(input('startTime')); //$startTime开始时间
        $endTime=strtotime(input('endTime'))+86400-1; //$endTime结束时间
        $contract_number=Request::instance()->post('contractNum'); //合同编号
        $voucher_number=Request::instance()->post('voucherNum'); //凭证号
        $core=Request::instance()->post('core'); //中心
        $abstract=Request::instance()->post('abstract'); //摘要
        $money_type=Request::instance()->post('moneyType'); //费用类别

        $where=[];
        if ($name!=null){
            array_push($where, ['username','=',$name]);
        }
        if ($startTime!=null){
            array_push($where, ['data','>=',$startTime]);
            array_push($where, ['data','<=',$endTime]);
        }
        if ($contract_number!=null){
            array_push($where, ['contract_number','=',$contract_number]);
        }
        if ($voucher_number!=null){
            array_push($where, ['voucher_number','=',$voucher_number]);
        }
        if ($core!=null){
            array_push($where, ['core','=',$core]);
        }
        if ($abstract!=null){
            array_push($where, ['abstract','=',$abstract]);
        }
        if ($money_type!=null){
            array_push($where, ['money_type','=',$money_type]);
        }

        $list=Db::name('account')->where($where)->select();

        if ($list){
            return json(['code'=>200,'data'=>$list]);
        }else{
            return json(['code'=>400,'msg'=>'没有该数据']);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值