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

 更新时间:2017年5月21日 22:11  点击:2247

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

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

HTML之中:

<form action="" method="get">
    <table class="account_table" width="100%" cellpadding="0" cellspacing="0">
      <tr>
        <td style="text-align:right">订单号:</td>
        <td>
          <input id="Orderid" name="order_sn" class="inp_wid3" type="text" value="{$_GET['order_sn']}"/>
        </td>
        <td style="text-align:right">
          下单日期:
        </td>
        <td colspan="5">
          <input type="text" class="inp_wid2" id="BeginTime" name="begintime" value="{$_GET['begintime']}" />
          至
          <input type="text" class="inp_wid2" id="EndTime" name="endtime" value="{$_GET['endtime']}" />
           交易完成日期
          <input type="text" class="inp_wid2" id="txtFinishedBeginTime" name="finishbegintime" value="{$_GET['finishbegintime']}" />
          至
          <input type="text" class="inp_wid2" id="txtFinishedEndTime" name="finishendtime" value="{$_GET['finishendtime']}" />
           订单金额:
          <input type="text" class="inp_wid2" id="txtMoneyMin" name="count_price_min" value="{$_GET['count_price_min']}"/>
          至
          <input type="text" class="inp_wid2" id="txtMoneyMax" name="count_price_max" value="{$_GET['count_price_max']}" />
        </td>
      </tr>
      <tr>
        <td style="text-align:right; width:80px">采购商名称:</td>
        <td style="width:140px">
          <input id="SupermarketName" name="user_nick_name" class="inp_wid3" type="text" value="{$_GET['user_nick_name']}" />
        </td>
        <td style="text-align:right; width:80px">采购商账号:</td>
        <td style="width:140px">
          <input id="SupermarketZh" name="user_name" class="inp_wid3" type="text" value="{$_GET['user_name']}" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input class="search_btn1" type="submit" value="搜索" id="Search" />
          </td>
      </tr>
    </table>
</form>

看到没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语句;你为何总是拼接错误呢!!!

<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
  private $page = "";
  public function getList($pagesize=25){
    $where = '1';
    $tableName = $this->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,实用于分页。

<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
  private $page = "";
  public function getList($pagesize=25){
    $where = '1';
    $tableName = $this->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程序设计有所帮助。

[!--infotagslink--]

相关文章

  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • thinkPHP中多维数组的遍历方法

    这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
  • Angular2搜索和重置按钮过场动画

    这篇文章主要介绍了Angular2搜索和重置按钮过场动画,需要的朋友可以参考下...2017-05-27
  • thinkphp自定义权限管理之名称判断方法

    下面小编就为大家带来一篇thinkphp自定义权限管理之名称判断方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • thinkphp怎么用?ThinkPHP使用方法

    本文详细介绍了ThinkPHP使用方法,不懂的同学快来跟小编一起看看吧 1、下载ThinkPHP模板,整个导入到项目根目录下。2、修改index.php文件,内容如下:<?php/***临时配置,...2017-07-06
  • thinkphp3.x连接mysql数据库的方法(具体操作步骤)

    这篇文章主要介绍了thinkphp3.x连接mysql数据库的方法,详细分析了thinkPHP3.x操作数据库的具体步骤,包括惯例配置文件设置,sql语句创建表以及针对数据库的数据插入操作等,需要的朋友可以参考下...2016-05-20
  • ThinkPHP 3.2.2实现事务操作的方法

    这篇文章主要介绍了ThinkPHP 3.2.2实现事务操作的方法,简单分析了thinkPHP中事务的启动、提交、回滚等操作方法并给出了完整的事务提交与回滚操作实例,需要的朋友可以参考下...2017-05-08
  • yii2带搜索功能的下拉框实例详解

    带搜索功能下拉框在项目中经常会用到,下面小编把实现代码分享到脚本之家平台,供大家参考...2016-05-13
  • ThinkPhP+Apache+PHPstorm整合框架流程图解

    这篇文章主要介绍了ThinkPhP+Apache+PHPstorm整合框架流程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-23
  • thinkPHP查询方式小结

    这篇文章主要介绍了thinkPHP查询方式,结合实例形式总结分析了thinkPHP的普通查询、表达式查询、区间查询、统计查询及SQL直接查询的使用技巧,需要的朋友可以参考下...2016-01-12
  • thinkPHP5.0框架整体架构总览【应用,模块,MVC,驱动,行为,命名空间等】

    这篇文章主要介绍了thinkPHP5.0框架整体架构,简单介绍了thinkPHP5.0的应用,模块,MVC,驱动,行为,命名空间等概念与基本用法,需要的朋友可以参考下...2017-04-03
  • thinkPHP商城公告功能开发问题分析

    这篇文章主要介绍了thinkPHP商城公告功能开发问题,结合实例形式分析了基于thinkPHP实现商城公告功能所涉及的ajax交互及数据库操作相关技巧,需要的朋友可以参考下...2016-12-02
  • Thinkphp事务操作实例(推荐)

    下面小编就为大家带来一篇Thinkphp事务操作实例(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • Nginx配置支持ThinkPHP的PATH_INFO

    这篇文章主要介绍了Nginx配置支持ThinkPHP的PATH_INFO,本文在Ubuntu的开发环境加配置成功,需要的朋友可以参考下...2016-01-27
  • PHP 搜索查询功能实现

    这篇文章主要介绍了PHP 搜索查询功能的实现代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-12-02
  • ThinkPHP使用getlist方法实现数据搜索功能示例

    这篇文章主要介绍了ThinkPHP使用getlist方法实现数据搜索功能,结合实例形式较为详细的分析了thinkPHP基于getlist实现根据给定条件进行数据的读取、显示等相关操作技巧,需要的朋友可以参考下...2017-05-21
  • Thinkphp自定义美化success和error提示跳转页面代码实例

    这篇文章主要介绍了Thinkphp自定义美化success和error提示跳转页面代码实例,有需要的同学可以直接借鉴文中代码,可以增加页面的美观和友好程度...2021-03-05
  • Thinkphp单字母函数使用指南

    本文给大家汇总介绍了thinkphp中的单字母函数的使用方法,非常的简单,他们都在 THINKPHPCommonfunctions.php 这个文件中 下面我分别说明一下他们的使用方法...2016-05-10
  • ThinkPHP Where 条件中常用表达式示例(详解)

    下面小编就为大家带来一篇ThinkPHP Where 条件中常用表达式示例(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • Angularjs material 实现搜索框功能

    这篇文章主要介绍了Angularjs material 实现搜索框功能的相关资料,需要的朋友可以参考下...2016-03-10