当前位置: 首页 > 编程笔记 >

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

蒋波光
2023-03-14
本文向大家介绍ThinkPHP使用getlist方法实现数据搜索功能示例,包括了ThinkPHP使用getlist方法实现数据搜索功能示例的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了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']}" />
          &nbsp;交易完成日期
          <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']}" />
          &nbsp;订单金额:
          <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程序设计有所帮助。

 类似资料:
  • 本文向大家介绍ThinkPHP实现ajax仿官网搜索功能实例,包括了ThinkPHP实现ajax仿官网搜索功能实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了ThinkPHP实现ajax仿官网搜索功能的方法。分享给大家供大家参考。 具体实现方法如下: 后台代码: 前端代码: 希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

  • 本文向大家介绍JS实现表格数据各种搜索功能的方法,包括了JS实现表格数据各种搜索功能的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS实现表格数据各种搜索功能。可忽略大小写,模糊搜索,多关键搜索。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的javascript程序设计有所帮助。

  • 本文向大家介绍layui实现显示数据表格、搜索和修改功能示例,包括了layui实现显示数据表格、搜索和修改功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了layui实现显示数据表格、搜索和修改功能。分享给大家供大家参考,具体如下: 引用样式和js,这是必须的。 后台返回数据格式 数据表格单元格数据修改 在表格初始化的时候加上 'edit':'text' 就可以编辑 然后再添加一个监

  • 本文向大家介绍thinkPHP实现签到功能的方法,包括了thinkPHP实现签到功能的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了thinkPHP实现签到功能的方法。分享给大家供大家参考,具体如下: 数据表: Controller: 更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用

  • 本文向大家介绍thinkphp实现分页显示功能,包括了thinkphp实现分页显示功能的使用技巧和注意事项,需要的朋友参考一下 先上效果图,突然发现和B站上一样 IndexController.class.php代码如下 index.html代码如下 css代码如下 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍layui数据表格实现重载数据表格功能(搜索功能),包括了layui数据表格实现重载数据表格功能(搜索功能)的使用技巧和注意事项,需要的朋友参考一下 layui数据表格实现重载数据表格功能,以搜索功能为例 加载数据表格 实现搜索功能和数据表格重载 全部代码 加载数据表格 按照layui官方文档示例 HTML部分 JavaScript部分 官方的文档已经把方法说的很明白了,下面展示实例