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


Posted in PHP onMay 08, 2017

本文实例讲述了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框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php你的验证码安全码?
Jan 02 PHP
php 表单数据的获取代码
Mar 10 PHP
浅谈PHP解析URL函数parse_url和parse_str
Nov 11 PHP
php隐藏IP地址后两位显示为星号的方法
Nov 21 PHP
PHP获取文件相对路径的方法
Feb 26 PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
May 12 PHP
ThinkPHP V2.2说明文档没有说明的那些事实例小结
Jul 01 PHP
php将金额数字转化为中文大写
Jul 09 PHP
PHP实现简单搜歌的方法
Jul 28 PHP
PHP模板引擎Smarty自定义变量调解器用法
Apr 11 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
Jun 30 PHP
PHP编程快速实现数组去重的方法详解
Jul 22 PHP
ThinkPHP实现图片上传操作的方法详解
May 08 #PHP
PHP开发中csrf攻击的简单演示和防范
May 07 #PHP
ThinkPHP框架实现数据增删改
May 07 #PHP
thinkphp 验证码 的使用小结
May 07 #PHP
解析 thinkphp 框架中的部分方法
May 07 #PHP
ThinkPHP 模板引擎使用详解
May 07 #PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 #PHP
You might like
PHP动态图像的创建
2006/10/09 PHP
Zend framework处理一个http请求的流程分析
2010/02/08 PHP
多个PHP中文字符串截取函数
2013/11/12 PHP
PHP程序员不应该忽略的3点
2015/10/09 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
postman的安装与使用方法(模拟Get和Post请求)
2018/08/06 PHP
php中yii框架实例用法
2020/12/22 PHP
js类型转换与引用类型详解(Boolean_Number_String)
2014/03/07 Javascript
jquery鼠标放上去显示悬浮层即弹出定位的div层
2014/04/25 Javascript
JavaScript中使用Callback控制流程介绍
2015/03/16 Javascript
JavaScript控制listbox列表框的项目上下移动的方法
2015/03/18 Javascript
JS排序方法(sort,bubble,select,insert)代码汇总
2016/01/30 Javascript
JS导出PDF插件的方法(支持中文、图片使用路径)
2016/07/12 Javascript
JavaScript实现大图轮播效果
2017/01/11 Javascript
JS实现图片放大镜插件详解
2017/11/06 Javascript
10个经典的网页鼠标特效代码
2018/01/09 Javascript
Nuxt.js实战详解
2018/01/18 Javascript
浅谈如何使用webpack构建多页面应用
2018/05/30 Javascript
使用微信SDK自定义分享的方法
2019/07/03 Javascript
JQuery事件委托(适用于给动态生成的脚本元素添加事件)
2020/02/01 jQuery
python调用机器喇叭发出蜂鸣声(Beep)的方法
2015/03/23 Python
python通过post提交数据的方法
2015/05/06 Python
Python实现登录人人网并抓取新鲜事的方法
2015/05/11 Python
Python中使用装饰器时需要注意的一些问题
2015/05/11 Python
Python二叉搜索树与双向链表转换算法示例
2019/03/02 Python
简单了解python代码优化小技巧
2019/07/08 Python
Levi’s美国官网:美国著名的牛仔裤品牌
2016/08/19 全球购物
香港礼品网站:GiftU eshop
2017/09/01 全球购物
英国蛋糕装饰用品一站式商店:Craft Company
2019/03/18 全球购物
经济实惠的名牌太阳镜和眼镜:Privé Revaux
2021/02/07 全球购物
县优秀教师事迹材料
2014/01/31 职场文书
学生党员检讨书范文
2014/12/27 职场文书
大学开学典礼新闻稿
2015/07/17 职场文书
创业计划书之农家乐
2019/10/09 职场文书
读《儒林外史》有感:少一些功利,多一些真诚
2020/01/19 职场文书
WINDOWS 64位 下安装配置mysql8.0.25最详细的教程
2022/03/22 MySQL