SoFunction
Updated on 2025-03-04

Example of ThinkPHP using getlist method to implement data search function

This article describes the use of the getlist method to implement data search function in ThinkPHP. Share it for your reference, as follows:

I write the getlist method in the model in ThinkPHP. In fact, the so-called search function is nothing more than like %string% used in database query, or other field names = specific value. These SQL statements are spliced ​​into and statements;

In HTML:

<form action="" method="get">
    <table class="account_table" width="100%" cellpadding="0" cellspacing="0">
      <tr>
        <td style="text-align:right">Order number:</td>
        <td>
          <input  name="order_sn" class="inp_wid3" type="text" value="{$_GET['order_sn']}"/>
        </td>
        <td style="text-align:right">
          Order date:
        </td>
        <td colspan="5">
          <input type="text" class="inp_wid2"  name="begintime" value="{$_GET['begintime']}" />
          to
          <input type="text" class="inp_wid2"  name="endtime" value="{$_GET['endtime']}" />
           Transaction completion date
          <input type="text" class="inp_wid2"  name="finishbegintime" value="{$_GET['finishbegintime']}" />
          to
          <input type="text" class="inp_wid2"  name="finishendtime" value="{$_GET['finishendtime']}" />
           Order amount:
          <input type="text" class="inp_wid2"  name="count_price_min" value="{$_GET['count_price_min']}"/>
          to
          <input type="text" class="inp_wid2"  name="count_price_max" value="{$_GET['count_price_max']}" />
        </td>
      </tr>
      <tr>
        <td style="text-align:right; width:80px">Buyer name:</td>
        <td style="width:140px">
          <input  name="user_nick_name" class="inp_wid3" type="text" value="{$_GET['user_nick_name']}" />
        </td>
        <td style="text-align:right; width:80px">Buyer account:</td>
        <td style="width:140px">
          <input  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="search"  />
          </td>
      </tr>
    </table>
</form>

I saw that there was no GET method to submit the form, this is the query condition filling option;

Among the controller:

$order_msg=$order->getList();
$this->assign('info',$order_msg);//This gets the order details

Among the Model:

public function getList($pagesize=25){
     $tableName = $this->getTableName();
   $where = $tableName.'.service_id = '.$_SESSION['service_site']['service_id'];
   if(!empty($_GET['order_sn'])){//Inquiry of order number       $where.= " and $tableName.`order_sn` like '%".$_GET['order_sn']."%'";
     }
   if(!empty($_GET['count_price_min'])){//Inquiry of the minimum order amount       $where.= " and $tableName.count_price >=".$_GET['count_price_min']."";
     }
   if(!empty($_GET['begintime'])){//Search for order start date    $_GET['begintime']=strtotime($_GET['begintime']);//Convert date to timestamp    $where.= " and $tableName.add_time >=".$_GET['begintime']."";
    $_GET['begintime']=date('Y-m-d',$_GET['begintime']);//Convert date to timestamp   }
   if(!empty($_GET['endtime'])){//Search for the end date of order     $_GET['endtime']=strtotime($_GET['endtime']);//Convert date to timestamp    $where.= " and $tableName.add_time <=".$_GET['endtime']."";
    $_GET['endtime']=date('Y-m-d',$_GET['endtime']);//Convert timestamps to dates to facilitate the front desk display after refreshing the page   }
   if(!empty($_GET['finishbegintime'])){//Search for transaction completion start date    $_GET['finishbegintime']=strtotime($_GET['finishbegintime']);//Convert date to timestamp    $where.= " and $tableName.ok_time >=".$_GET['finishbegintime']."";
    $_GET['finishbegintime']=date('Y-m-d',$_GET['finishbegintime']);//Convert date to timestamp   }
   if(!empty($_GET['finishendtime'])){//Search for transaction completion end date     $_GET['finishendtime']=strtotime($_GET['finishendtime']);//Convert date to timestamp    $where.= " and $tableName.ok_time <=".$_GET['finishendtime']."";
    $_GET['finishendtime']=date('Y-m-d',$_GET['finishendtime']);//Convert timestamps to dates to facilitate the front desk display after refreshing the page   }
   if(!empty($_GET['send'])){//Inquiry of shipped warning orders, the delivery time is more than five days away    $where.= " and $tableName.send_time < '".(time()-60*60*24*5)."'";
   }
   if(!empty($_GET['doingorder'])){//Inquiry orders    $where.= " and $ in (0,1)";
   }
   if(!empty($_GET['warningorder'])){//Inquiry and warning order: payment has been made and has not been shipped for more than 24 hours    $where.= " and $tableName.pay_time < '".(time()-60*60*24)."'";
   }
   if(!empty($_GET['warningorder'])){//Inquiry and warning order: payment has been made and has not been shipped for more than 24 hours    $where.= " and $tableName.is_pay = 1 ";
   }
   if(!empty($_GET['warningorder'])){//Inquiry and warning order: payment has been made and has not been shipped for more than 24 hours   $where.= " and $ in (0,1)";
   }
   if(!empty($_GET['count_price_max'])){//Inquiry of the maximum amount of order    $where.= " and $tableName.count_price <=".$_GET['count_price_max']."";
   }
   if(!empty($_GET['user_nick_name'])){//Query the buyer name    $where.= " and fab_user.nick_name like '".$_GET['user_nick_name']."%'";
   }
   if(!empty($_GET['user_name'])){//Check the buyer account    $where.= " and fab_user.user_name like '".$_GET['user_name']."%'";
   }
   if(!empty($_GET['supplier_nick_name'])){//Query the supplier's name    $where.= " and fab_supplier.nick_name like '".$_GET['supplier_nick_name']."%'";
   }
   if(!empty($_GET['supplier_name'])){//Check the supplier account    $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){//Inquiry on payment methods    $where.= " and fab_order_info.pay_type = ".$_GET['pay_type']."";
   }
   if(($_GET['status'])!=""&&($_GET['status'])!=-1){//Check the order status    $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);//Order details}

You just need to pay attention to the GET data acquisition and then splice SQL statements; why do you always splice errors! ! !

<?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;
  }
}

A streamlined universal version of getlist, practical for pagination.

<?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;
  }
}

Lite version of MODEL for automatic data verification

For more information about thinkPHP related content, please check out the topic of this site:ThinkPHP Introduction Tutorial》、《Summary of the operation skills of thinkPHP templates》、《Summary of common methods of ThinkPHP》、《Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Zend FrameWork Framework Introduction Tutorial"and"PHP template technical summary》。

I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.