Maison  >  Article  >  développement back-end  >  Analyse de l'utilisation par ThinkPHP de la méthode getlist pour implémenter la fonction de recherche de données

Analyse de l'utilisation par ThinkPHP de la méthode getlist pour implémenter la fonction de recherche de données

不言
不言original
2018-06-08 11:58:511937parcourir

Cet article présente principalement l'utilisation par ThinkPHP de la méthode getlist pour implémenter la fonction de recherche de données. Il analyse en détail sous forme d'exemples, l'implémentation par thinkPHP de la lecture et de l'affichage des données en fonction de conditions données basées sur getlist et d'autres techniques d'exploitation associées. Les amis qui en ont besoin peuvent Pour référence,

L'exemple de cet article décrit comment ThinkPHP utilise la méthode getlist pour implémenter la fonction de recherche de données. Partagez-le avec tout le monde pour votre référence, les détails sont les suivants :

J'écris la méthode getlist dans le modèle dans ThinkPHP. En fait, la soi-disant fonction de recherche n'est rien de plus que %string%, ou. other utilisé dans les requêtes de base de données Le nom du champ = valeur spécifique, ces instructions SQL sont fusionnées dans l'instruction and

En 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[&#39;order_sn&#39;]}"/>
        </td>
        <td style="text-align:right">
          下单日期:
        </td>
        <td colspan="5">
          <input type="text" class="inp_wid2" id="BeginTime" name="begintime" value="{$_GET[&#39;begintime&#39;]}" />
          至
          <input type="text" class="inp_wid2" id="EndTime" name="endtime" value="{$_GET[&#39;endtime&#39;]}" />
           交易完成日期
          <input type="text" class="inp_wid2" id="txtFinishedBeginTime" name="finishbegintime" value="{$_GET[&#39;finishbegintime&#39;]}" />
          至
          <input type="text" class="inp_wid2" id="txtFinishedEndTime" name="finishendtime" value="{$_GET[&#39;finishendtime&#39;]}" />
           订单金额:
          <input type="text" class="inp_wid2" id="txtMoneyMin" name="count_price_min" value="{$_GET[&#39;count_price_min&#39;]}"/>
          至
          <input type="text" class="inp_wid2" id="txtMoneyMax" name="count_price_max" value="{$_GET[&#39;count_price_max&#39;]}" />
        </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[&#39;user_nick_name&#39;]}" />
        </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[&#39;user_name&#39;]}" />
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input class="search_btn1" type="submit" value="搜索" id="Search" />
          </td>
      </tr>
    </table>
</form>

Voir Il n'y a pas de méthode GET pour soumettre le formulaire, il s'agit de l'option de remplissage des conditions de requête dans le contrôleur

$order_msg=$order->getList();
$this->assign(&#39;info&#39;,$order_msg);//这个获取订单的详细信息
Dans le modèle :

public function getList($pagesize=25){
     $tableName = $this->getTableName();
   $where = $tableName.&#39;.service_id = &#39;.$_SESSION[&#39;service_site&#39;][&#39;service_id&#39;];
   if(!empty($_GET[&#39;order_sn&#39;])){//查询订单号
       $where.= " and $tableName.`order_sn` like &#39;%".$_GET[&#39;order_sn&#39;]."%&#39;";
     }
   if(!empty($_GET[&#39;count_price_min&#39;])){//查询订单最小金额
       $where.= " and $tableName.count_price >=".$_GET[&#39;count_price_min&#39;]."";
     }
   if(!empty($_GET[&#39;begintime&#39;])){//下单开始日期搜索
    $_GET[&#39;begintime&#39;]=strtotime($_GET[&#39;begintime&#39;]);//将日期转为时间戳
    $where.= " and $tableName.add_time >=".$_GET[&#39;begintime&#39;]."";
    $_GET[&#39;begintime&#39;]=date(&#39;Y-m-d&#39;,$_GET[&#39;begintime&#39;]);//将日期转为时间戳
   }
   if(!empty($_GET[&#39;endtime&#39;])){//下单结束日期搜索
     $_GET[&#39;endtime&#39;]=strtotime($_GET[&#39;endtime&#39;]);//将日期转为时间戳
    $where.= " and $tableName.add_time <=".$_GET[&#39;endtime&#39;]."";
    $_GET[&#39;endtime&#39;]=date(&#39;Y-m-d&#39;,$_GET[&#39;endtime&#39;]);//将时间戳转换成日期,方便刷新页面后前台显示
   }
   if(!empty($_GET[&#39;finishbegintime&#39;])){//交易完成开始日期搜索
    $_GET[&#39;finishbegintime&#39;]=strtotime($_GET[&#39;finishbegintime&#39;]);//将日期转为时间戳
    $where.= " and $tableName.ok_time >=".$_GET[&#39;finishbegintime&#39;]."";
    $_GET[&#39;finishbegintime&#39;]=date(&#39;Y-m-d&#39;,$_GET[&#39;finishbegintime&#39;]);//将日期转为时间戳
   }
   if(!empty($_GET[&#39;finishendtime&#39;])){//交易完成结束日期搜索
     $_GET[&#39;finishendtime&#39;]=strtotime($_GET[&#39;finishendtime&#39;]);//将日期转为时间戳
    $where.= " and $tableName.ok_time <=".$_GET[&#39;finishendtime&#39;]."";
    $_GET[&#39;finishendtime&#39;]=date(&#39;Y-m-d&#39;,$_GET[&#39;finishendtime&#39;]);//将时间戳转换成日期,方便刷新页面后前台显示
   }
   if(!empty($_GET[&#39;send&#39;])){//查询已发货预警订单,发货时间距离此刻超过五天
    $where.= " and $tableName.send_time < &#39;".(time()-60*60*24*5)."&#39;";
   }
   if(!empty($_GET[&#39;doingorder&#39;])){//查询处理中的订单
    $where.= " and $tableName.status in (0,1)";
   }
   if(!empty($_GET[&#39;warningorder&#39;])){//查询预警的订单:已经付款且时间超过24小时未发货
    $where.= " and $tableName.pay_time < &#39;".(time()-60*60*24)."&#39;";
   }
   if(!empty($_GET[&#39;warningorder&#39;])){//查询预警的订单:已经付款且时间超过24小时未发货
    $where.= " and $tableName.is_pay = 1 ";
   }
   if(!empty($_GET[&#39;warningorder&#39;])){//查询预警的订单:已经付款且时间超过24小时未发货
   $where.= " and $tableName.status in (0,1)";
   }
   if(!empty($_GET[&#39;count_price_max&#39;])){//查询订单最大金额
    $where.= " and $tableName.count_price <=".$_GET[&#39;count_price_max&#39;]."";
   }
   if(!empty($_GET[&#39;user_nick_name&#39;])){//查询采购商名称
    $where.= " and fab_user.nick_name like &#39;".$_GET[&#39;user_nick_name&#39;]."%&#39;";
   }
   if(!empty($_GET[&#39;user_name&#39;])){//查询采购商账号
    $where.= " and fab_user.user_name like &#39;".$_GET[&#39;user_name&#39;]."%&#39;";
   }
   if(!empty($_GET[&#39;supplier_nick_name&#39;])){//查询供应商商名称
    $where.= " and fab_supplier.nick_name like &#39;".$_GET[&#39;supplier_nick_name&#39;]."%&#39;";
   }
   if(!empty($_GET[&#39;supplier_name&#39;])){//查询供应商账号
    $where.= " and fab_supplier.supplier_name like &#39;".$_GET[&#39;supplier_name&#39;]."%&#39;";
   }
   if($_GET[&#39;history&#39;] == 1){
     $where .= " and {$tableName}.status in (2,3,4) ";
   }
   if(($_GET[&#39;pay_type&#39;])!=""&&($_GET[&#39;pay_type&#39;])!=-1){//查询支付方式
    $where.= " and fab_order_info.pay_type = ".$_GET[&#39;pay_type&#39;]."";
   }
   if(($_GET[&#39;status&#39;])!=""&&($_GET[&#39;status&#39;])!=-1){//查询订单状态
    $where.= " and fab_order_info.status = ".$_GET[&#39;status&#39;]."";
   }
     if(!empty($_GET[&#39;stime&#39;]) && !empty($_GET[&#39;etime&#39;])){
       $stime = strtotime($_GET[&#39;stime&#39;]);
       $etime = strtotime($_GET[&#39;etime&#39;]) + 24*60*60;
       $where.= " and ($tableName.`inputtime` between &#39;$stime&#39; and &#39;$etime&#39;)";
     }
     $count = $this->where($where)->count();
     $this->countNum = $count;
     $Page = new \Think\Page($count,$pagesize);
     $this->page = $Page->show();
     $limit = $Page->firstRow.&#39;,&#39;.$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);//订单详细信息
}
Il vous suffit de faire attention à l'acquisition de données GET, puis de coller l'instruction SQL, pourquoi ; épissez toujours incorrectement ! ! !

<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
  private $page = "";
  public function getList($pagesize=25){
    $where = &#39;1&#39;;
    $tableName = $this->getTableName();
    $count = $this->where($where)->count();
    $Page = new \Think\Page($count,$pagesize);
    $this->page = $Page->show();
    $limit = $Page->firstRow.&#39;,&#39;.$Page->listRows;
    return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
  }
  public function getPage(){
    return $this->page;
  }
}
Une version universelle simplifiée de getlist, utile pour la pagination.

<?php
namespace Admin\Model;
use Think\Model;
class KuaidicompanyModel extends Model {
  private $page = "";
  public function getList($pagesize=25){
    $where = &#39;1&#39;;
    $tableName = $this->getTableName();
    $count = $this->where($where)->count();
    $Page = new \Think\Page($count,$pagesize);
    $this->page = $Page->show();
    $limit = $Page->firstRow.&#39;,&#39;.$Page->listRows;
    return $this->query("select * from $tableName where $where order by $tableName.`id` asc limit $limit ");
  }
  public function getPage(){
    return $this->page;
  }
}

Version allégée de MODEL pour la vérification automatique des données

C'est tout pour cet article L'intégralité du contenu, j'espère qu'il sera utile à l'étude de chacun. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois !

Recommandations associées :

Analyse du filigrane ThinkPHP et définition de la position du filigrane


À propos du framework thinkphp pour implémenter l'ajout de données et l'affichage méthodes fonctionnelles


Analyse du code source de la méthode setInc dans thinkphp3.2.0


Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn