欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 08:23:35
本文实例讲述了thinkphp使用getlist方法实现数据搜索功能。分享给大家供大家参考,具体如下: 自己在thinkphp之中的model之中书写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']}" />
           交易完成日期
          <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程序设计有所帮助。