Analysis on thinkphp using getlist method to realize data searching function

Source: Internet
Author: User
Tags timestamp to date
This article mainly introduces the thinkphp using GetList method to achieve data search function, combined with the case form more detailed analysis of thinkphp based on the getlist implementation of the data according to the given conditions of the reading, display and other related operations skills, the need for friends can refer to the next

The example in this paper describes thinkphp using GetList method to realize the data search function. Share to everyone for your reference, as follows:

Oneself in the model among thinkphp writing GetList method, in fact, the so-called search function is nothing more than the database query used like%string%, or other field names = specific values, these SQL statements are stitched together in and statements;

Within 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 id= "Orderid" 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=" in P_wid2 "id=" BeginTime "name=" BeginTime "value=" {$_get[' BeginTime ']} "/> to <input type=" text "class = "Inp_wid2" id= "EndTime" name= "EndTime" value= "{$_get[' EndTime ']}"/> Transaction finish date <input type= "text" C          lass= "Inp_wid2" id= "Txtfinishedbegintime" name= "Finishbegintime" value= "{$_get[' finishbegintime ']}"/> to <input type= "text" class= "Inp_wid2" id= "Txtfinishedendtime" name= "Finishendtime" value= "{$_get[' Finishendtime ']}    "/> Order Amount:      <input type= "text" class= "Inp_wid2" id= "Txtmoneymin" name= "Count_price_min" value= "{$_get[' count_price_min ']}"/&          Gt To <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 "> Buyer name:</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=" Te Xt-align:right; width:80px "> Buyer account:</td> <td style=" width:140px "> <input id=" Supermarketzh "Name=" User_na        Me "class=" inp_wid3 "type=" text "value=" {$_get[' user_name ']} "/> </td> </tr> <tr> &LT;TD colspan= "2" > <input class= "search_btn1" type= "submit" value= "search" id= "searches"/> &lt ;/td> </tr> </table></form> 

See no get method to submit the form, this is the query condition fill in the option;

Controller:

$order _msg= $order->getlist () $this->assign (' info ', $order _msg);//This gets the details of the order

Model:

Public Function getList ($pagesize =25) {$tableName = $this->gettablename (); $where = $tableName. '.   service_id = '. $_session[' service_site ' [' service_id ']; if (!empty ($_get[' order_sn '))) {//query order number $where. = "and $tableName. ' order_sn ' like '%". $_get[' Order_sn ']. "     %'"; } if (!empty ($_get[' count_price_min ')) {//Query order Minimum Amount $where. = "and $tableName. Count_price >=". $_get[' Count_price_     Min ']. "";  } if (!empty ($_get[' begintime ')) {//Order start date Search $_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 ')) {//order end Date Search $_get[' E    Ndtime ']=strtotime ($_get[' endtime ');//convert date to timestamp $where. = "and $tableName. Add_time <=". $_get[' Endtime ']. "; $_get[' Endtime ']=date (' y-m-d ', $_get[' endtime ']);//convert timestamp to date, convenient to refresh page after foreground display} if (!empty ($_get[' finishbegintime ')) {/ /Transaction Completion start date search $_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 '])) { Transaction completion End Date Search $_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 timestamp to date, convenient to refresh the page after the foreground display} if (!empty ($_get[' send ')) { Check for dispatched alert orders, shipping time is longer than five days $where. = "and $tableName. Send_time < '".   (Time () -60*60*24*5). "'";   } if (!empty ($_get[' Doingorder ')) {//order $where in query processing. = "and $tableName. Status in (0,1)"; } if (!empty ($_get[' Warningorder ')) {//Query alerts for orders: $where that have been paid and have not shipped for more than 24 hours. = "and $tableName. Pay_time < '". (   Time () -60*60*24). "'";   } if (!empty ($_get[' Warningorder ')) {//Query alerts for orders: $where that have been paid and have not shipped for more than 24 hours. = "and $tableName. Is_pay = 1"; } if (!empty ($_get[' Warningorder ')) {//Query alerts for orders: already paid andTime exceeds 24 hours $where. = "and $tableName. Status in (0,1)"; } if (!empty ($_get[' Count_price_max ')) {//Query the order maximum amount $where. = "and $tableName. Count_price <=". $_get[' Count_price_ma   X ']. ""; } if (!empty ($_get[' user_nick_name ')) {//Query the purchaser name $where. = "and fab_user.nick_name like '". $_get[' User_nick_name ']. "   %'"; } if (!empty ($_get[' user_name ')) {//Query the buyer account $where. = "and fab_user.user_name like '". $_get[' user_name ']. "   %'"; } if (!empty ($_get[' supplier_nick_name ')) {//Query vendor name $where. = "and fab_supplier.nick_name like '". $_get[' Supplier_ni Ck_name ']. "   %'"; } if (!empty ($_get[' supplier_name ')) {//Query the vendor 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 ']) {//Query payment method $where. = "and!=-1 =". Fab_order_info.pay_type   get[' Pay_type ']. "; } if (($_get[' status ')! = "" && ($_get[' status '])!=-1) {Query 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 in fab_order_info.supplier_id=fab_supplier.supplier_id) left join Fab_user on Fab_orde    R_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 the 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 get, and then splicing SQL statements; Why are you always splicing errors?!!

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

Streamline the universal version of GetList, which is actually used for paging.

<?phpnamespace 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 model for automatic data validation

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.