Home>Article>Backend Development> Analysis on ThinkPHP's use of getlist method to implement data search function
This article mainly introduces ThinkPHP's use of the getlist method to implement data search functions. It analyzes in detail in the form of examples, thinkPHP's implementation of reading and displaying data based on given conditions based on getlist and other related operating techniques. Friends who need it can Refer to the following
The example of this article describes ThinkPHP's use of the getlist method to implement the data search function. Share it with everyone for your reference, the details are 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%, or other used in database queries. The field name = specific value, these sql statements are spliced into the and statement;
In HTML:
I see no GET Method to submit the form, this is the query condition filling option;
In the controller:
$order_msg=$order->getList(); $this->assign('info',$order_msg);//这个获取订单的详细信息
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'])){//查询订单号 $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);//订单详细信息 }
You only need to pay attention to the GET data acquisition, and then splice the SQL statement; why do you always splice incorrectly! ! !
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; } }
Simplified general version of getlist, suitable for paging.
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 streamlined version of MODEL is used for automatic data verification
The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Analysis on ThinkPHP watermarking and setting watermark position
About thinkphp framework to implement data addition and Displayed functional methods
Source code analysis of setInc method in thinkphp3.2.0
The above is the detailed content of Analysis on ThinkPHP's use of getlist method to implement data search function. For more information, please follow other related articles on the PHP Chinese website!