侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

ThinkPHP使用getlist方法实现数据搜索功能示例

ThinkPHP  /  管理员 发布于 8年前   207

本文实例讲述了ThinkPHP使用getlist方法实现数据搜索功能。分享给大家供大家参考,具体如下:

自己在ThinkPHP之中的model之中书写getlist方法,其实所谓的搜索功能无非就是数据库查询之中用到的like  %string%,或者其他的 字段名=特定值,这些sql语句拼接在and语句之中;

HTML之中:

订单号: 下单日期: 至  交易完成日期 至  订单金额: 至
采购商名称: 采购商账号:

看到没GET方法提交表单,这个是查询条件填入选项;

控制器之中:

$order_msg=$order->getList();$this->assign('info',$order_msg);//这个获取订单的详细信息

Model之中:

public function getList($pagesize=25){     $tableName = $this->getTableName();   $where = $tableName.'.service_id = '.$_SESSION['service_site']['service_id'];   if(!empty($_GET['order_sn'])){//查询订单号       $where.= " and $tableName.`order_sn` like '%".$_GET['order_sn']."%'";     }   if(!empty($_GET['count_price_min'])){//查询订单最小金额       $where.= " and $tableName.count_price >=".$_GET['count_price_min']."";     }   if(!empty($_GET['begintime'])){//下单开始日期搜索    $_GET['begintime']=strtotime($_GET['begintime']);//将日期转为时间戳    $where.= " and $tableName.add_time >=".$_GET['begintime']."";    $_GET['begintime']=date('Y-m-d',$_GET['begintime']);//将日期转为时间戳   }   if(!empty($_GET['endtime'])){//下单结束日期搜索     $_GET['endtime']=strtotime($_GET['endtime']);//将日期转为时间戳    $where.= " and $tableName.add_time <=".$_GET['endtime']."";    $_GET['endtime']=date('Y-m-d',$_GET['endtime']);//将时间戳转换成日期,方便刷新页面后前台显示   }   if(!empty($_GET['finishbegintime'])){//交易完成开始日期搜索    $_GET['finishbegintime']=strtotime($_GET['finishbegintime']);//将日期转为时间戳    $where.= " and $tableName.ok_time >=".$_GET['finishbegintime']."";    $_GET['finishbegintime']=date('Y-m-d',$_GET['finishbegintime']);//将日期转为时间戳   }   if(!empty($_GET['finishendtime'])){//交易完成结束日期搜索     $_GET['finishendtime']=strtotime($_GET['finishendtime']);//将日期转为时间戳    $where.= " and $tableName.ok_time <=".$_GET['finishendtime']."";    $_GET['finishendtime']=date('Y-m-d',$_GET['finishendtime']);//将时间戳转换成日期,方便刷新页面后前台显示   }   if(!empty($_GET['send'])){//查询已发货预警订单,发货时间距离此刻超过五天    $where.= " and $tableName.send_time < '".(time()-60*60*24*5)."'";   }   if(!empty($_GET['doingorder'])){//查询处理中的订单    $where.= " and $tableName.status in (0,1)";   }   if(!empty($_GET['warningorder'])){//查询预警的订单:已经付款且时间超过24小时未发货    $where.= " and $tableName.pay_time < '".(time()-60*60*24)."'";   }   if(!empty($_GET['warningorder'])){//查询预警的订单:已经付款且时间超过24小时未发货    $where.= " and $tableName.is_pay = 1 ";   }   if(!empty($_GET['warningorder'])){//查询预警的订单:已经付款且时间超过24小时未发货   $where.= " and $tableName.status in (0,1)";   }   if(!empty($_GET['count_price_max'])){//查询订单最大金额    $where.= " and $tableName.count_price <=".$_GET['count_price_max']."";   }   if(!empty($_GET['user_nick_name'])){//查询采购商名称    $where.= " and fab_user.nick_name like '".$_GET['user_nick_name']."%'";   }   if(!empty($_GET['user_name'])){//查询采购商账号    $where.= " and fab_user.user_name like '".$_GET['user_name']."%'";   }   if(!empty($_GET['supplier_nick_name'])){//查询供应商商名称    $where.= " and fab_supplier.nick_name like '".$_GET['supplier_nick_name']."%'";   }   if(!empty($_GET['supplier_name'])){//查询供应商账号    $where.= " and fab_supplier.supplier_name like '".$_GET['supplier_name']."%'";   }   if($_GET['history'] == 1){     $where .= " and {$tableName}.status in (2,3,4) ";   }   if(($_GET['pay_type'])!=""&&($_GET['pay_type'])!=-1){//查询支付方式    $where.= " and fab_order_info.pay_type = ".$_GET['pay_type']."";   }   if(($_GET['status'])!=""&&($_GET['status'])!=-1){//查询订单状态    $where.= " and fab_order_info.status = ".$_GET['status']."";   }     if(!empty($_GET['stime']) && !empty($_GET['etime'])){       $stime = strtotime($_GET['stime']);       $etime = strtotime($_GET['etime']) + 24*60*60;       $where.= " and ($tableName.`inputtime` between '$stime' and '$etime')";     }     $count = $this->where($where)->count();     $this->countNum = $count;     $Page = new \Think\Page($count,$pagesize);     $this->page = $Page->show();     $limit = $Page->firstRow.','.$Page->listRows;    $sql="select $tableName.*,fab_supplier.nick_name as supplier_nick_name,fab_user.nick_name as user_nick_name    from ($tableName left join fab_supplier on fab_order_info.supplier_id=fab_supplier.supplier_id)    left join fab_user on fab_order_info.user_id=fab_user.user_id where $where order by $tableName.`order_id` desc limit $limit";    $sqls="select sum(fab_order_info.count_price) as order_price,count(fab_order_info.count_price) as order_count    from $tableName where $where order by $tableName.`order_id` desc limit $limit";    $this->sql_msg=$this->query($sqls);    return $this->query($sql);//订单详细信息}

你只需要留意那个GET数据获取,然后进行拼接SQL语句;你为何总是拼接错误呢!!!

getTableName();    $count = $this->where($where)->count();    $Page = new \Think\Page($count,$pagesize);    $this->page = $Page->show();    $limit = $Page->firstRow.','.$Page->listRows;    return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");  }  public function getPage(){    return $this->page;  }}

精简通用版getlist,实用于分页。

getTableName();    $count = $this->where($where)->count();    $Page = new \Think\Page($count,$pagesize);    $this->page = $Page->show();    $limit = $Page->firstRow.','.$Page->listRows;    return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");  }  public function getPage(){    return $this->page;  }}

精简版MODEL用于数据自动验证

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • ThinkPHP实现ajax仿官网搜索功能实例
  • Thinkphp搜索时首页分页和搜索页保持条件分页的方法
  • ThinkPHP让分页保持搜索状态的方法
  • ThinkPHP设置禁止百度等搜索引擎转码(简单实用)
  • php+ajax实时输入自动搜索匹配的方法
  • php+ajax做仿百度搜索下拉自动提示框(有实例)
  • ajax+php 谷歌搜索框自动填充功能 实例代码
  • php+ajax实现仿百度查询下拉内容功能示例
  • thinkPHP+mysql+ajax实现的仿百度一下即时搜索效果详解


  • 上一条:
    thinkPHP5 tablib标签库自定义方法详解
    下一条:
    ThinkPHP实现图片上传操作的方法详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客