ThinkPHP uses the getlist method to implement the data search function example, thinkphpgetlist
This example describes how ThinkPHP uses the getlist method to search data. We will share this with you for your reference. The details are as follows:
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 queries, or other field names = specific values, these SQL statements are concatenated 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> <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 = "inp_wid2" id = "BeginTime" name = "begintime" value = "{$ _ GET ['begintime']} "/> to <input type =" text "class =" inp_wid2 "id =" EndTime "name =" endtime "value =" {$ _ GET ['endtime']} "/> Transaction completion date <input type =" text "class =" 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']}"/> to <input type = "text" class = "inp_wid2" id = "txtMoneyMax" name = "count_price_max" value = "{$ _ GET ['count _ price_max ']}"/> </td> </tr> <td style = "text-align: right; width: 80px "> purchaser 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 =" text-align: right; width: 80px "> buyer account: </td> <td style =" width: 140px "> <input id =" SupermarketZh "name =" user_name "class =" inp_wid3 "type =" text "value =" {$ _ GET ['user _ name']} "/> </td> </tr> <td colspan =" 2 "> <input class =" search_btn1 "type =" submit "value =" Search "id = "Search"/> </td> </tr> </table> </form>
No GET method submitted form is displayed. This is the query condition filling option;
In the controller:
$ Order_msg = $ order-> getList (); $ this-> assign ('info', $ order_msg); // obtain the order Details
In the Model:
Public function getList ($ pagesize = 25) {$ tableName = $ this-> getTableName (); $ where = $ tableName. '. service_id = '. $ _ SESSION ['service _ site'] ['service _ id']; if (! Empty ($ _ GET ['order _ sn ']) {// query the order number $ where. = "and $ tableName. 'order _ sn 'like' % ". $ _ GET ['order _ sn ']. "% '";} if (! Empty ($ _ GET ['count _ price_min ']) {// query the minimum order amount $ where. = "and $ tableName. count_price> = ". $ _ GET ['count _ price_min ']. "";} if (! Empty ($ _ GET ['begintime']) {// search for the order start date $ _ GET ['begintime'] = strtotime ($ _ GET ['begintime']); // convert the date to the timestamp $ where. = "and $ tableName. add_time> = ". $ _ GET ['begintime']. ""; $ _ GET ['begintime'] = date ('Y-m-d', $ _ GET ['begintime']); // convert the date to the timestamp} if (! Empty ($ _ GET ['endtime']) {// end date of an order search $ _ GET ['endtime'] = strtotime ($ _ GET ['endtime']); // convert the date to the timestamp $ where. = "and $ tableName. add_time <= ". $ _ GET ['endtime']. ""; $ _ GET ['endtime'] = date ('Y-m-d', $ _ GET ['endtime']); // convert the timestamp into a date, which is displayed at the front end after the page is refreshed.} if (! Empty ($ _ GET ['finishbegintime']) {// start date of Transaction completion search $ _ GET ['finishbegintime'] = strtotime ($ _ GET ['finishbegintime']); // convert the date to the timestamp $ where. = "and $ tableName. OK _time> = ". $ _ GET ['finishbegintime']. ""; $ _ GET ['finishbegintime'] = date ('Y-m-d', $ _ GET ['finishbegintime']); // convert the date to the timestamp} if (! Empty ($ _ GET ['finishendtime']) {// search for the Transaction completion end date $ _ GET ['finishendtime'] = strtotime ($ _ GET ['finishendtime']); // convert the date to the timestamp $ where. = "and $ tableName. OK _time <= ". $ _ GET ['finishendtime']. ""; $ _ GET ['finishendtime'] = date ('Y-m-d', $ _ GET ['finishendtime']); // convert the timestamp into a date, which is displayed at the front end after the page is refreshed.} if (! Empty ($ _ GET ['send']) {// query the delivery warning order. The delivery time is more than five days from the moment. $ where. = "and $ tableName. send_time <'". (time ()-60*60*24*5 ). "'";} if (! Empty ($ _ GET ['domainorder']) {// query the order in processing $ where. = "and $ tableName. status in ()";} if (! Empty ($ _ GET ['warningorder']) {// query the warning order: the order has been paid and has not been delivered for more than 24 hours $ where. = "and $ tableName. pay_time <'". (time ()-60*60*24 ). "'";} if (! Empty ($ _ GET ['warningorder']) {// query the warning order: the order has been paid and has not been delivered for more than 24 hours $ where. = "and $ tableName. is_pay = 1 ";} if (! Empty ($ _ GET ['warningorder']) {// query the warning order: the order has been paid and has not been delivered for more than 24 hours $ where. = "and $ tableName. status in (0, 1) ";} if (! Empty ($ _ GET ['count _ price_max ']) {// query the maximum order amount $ where. = "and $ tableName. count_price <= ". $ _ GET ['count _ price_max ']. "";} 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's account $ where. = "and fab_user.user_name like '". $ _ GET ['user _ name']. "% '";} if (! Empty ($ _ GET ['Supplier _ nick_name ']) {// query supplier name $ where. = "and fab_supplier.nick_name like '". $ _ GET ['Supplier _ nick_name ']. "% '";} if (! Empty ($ _ GET ['Supplier _ name']) {// query 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) {// query the payment method $ where. = "and 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. *, using as supplier_nick_name, fab_user.nick_name as user_nick_name from ($ tableName left join fab_supplier on outer = outer) left join fab_user on outer = 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 only need to pay attention to the GET data and then splice 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; }}
The simplified general version of getlist is 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 verification