标签:
自己在ThinkPHP之中的model之中书写getlist方法,其实所谓的搜索功能无非就是数据库查询之中用到的like %string%,或者其他的 字段名=特定值,这些sql语句拼接在and语句之中;
HTML之中:
1 <form action="" method="get"> 2 <table class="account_table" width="100%" cellpadding="0" cellspacing="0"> 3 <tr> 4 <td style="text-align:right">订单号:</td> 5 <td> 6 <input id="Orderid" name="order_sn" class="inp_wid3" type="text" value="{$_GET[‘order_sn‘]}"/> 7 </td> 8 <td style="text-align:right"> 9 下单日期: 10 </td> 11 12 <td colspan="5"> 13 <input type="text" class="inp_wid2" id="BeginTime" name="begintime" value="{$_GET[‘begintime‘]}" /> 14 至 15 <input type="text" class="inp_wid2" id="EndTime" name="endtime" value="{$_GET[‘endtime‘]}" /> 16 交易完成日期 17 <input type="text" class="inp_wid2" id="txtFinishedBeginTime" name="finishbegintime" value="{$_GET[‘finishbegintime‘]}" /> 18 至 19 <input type="text" class="inp_wid2" id="txtFinishedEndTime" name="finishendtime" value="{$_GET[‘finishendtime‘]}" /> 20 订单金额: 21 <input type="text" class="inp_wid2" id="txtMoneyMin" name="count_price_min" value="{$_GET[‘count_price_min‘]}"/> 22 至 23 <input type="text" class="inp_wid2" id="txtMoneyMax" name="count_price_max" value="{$_GET[‘count_price_max‘]}" /> 24 </td> 25 </tr> 26 <tr> 27 <td style="text-align:right; width:80px">采购商名称:</td> 28 <td style="width:140px"> 29 <input id="SupermarketName" name="user_nick_name" class="inp_wid3" type="text" value="{$_GET[‘user_nick_name‘]}" /> 30 </td> 31 <td style="text-align:right; width:80px">采购商账号:</td> 32 <td style="width:140px"> 33 <input id="SupermarketZh" name="user_name" class="inp_wid3" type="text" value="{$_GET[‘user_name‘]}" /> 34 </td> 39 </tr> 40 41 <tr> 42 <td colspan="2"> 43 <input class="search_btn1" type="submit" value="搜索" id="Search" /> 44 </td> 45 </tr> 46 </table> 47 </form>
看到没GET方法提交表单,这个是查询条件填入选项;
控制器之中:
1 $order_msg=$order->getList(); 2 $this->assign(‘info‘,$order_msg);//这个获取订单的详细信息
Model之中:
1 public function getList($pagesize=25){ 2 3 $tableName = $this->getTableName(); 4 $where = $tableName.‘.service_id = ‘.$_SESSION[‘service_site‘][‘service_id‘]; 5 if(!empty($_GET[‘order_sn‘])){//查询订单号 6 $where.= " and $tableName.`order_sn` like ‘%".$_GET[‘order_sn‘]."%‘"; 7 } 8 9 if(!empty($_GET[‘count_price_min‘])){//查询订单最小金额 10 $where.= " and $tableName.count_price >=".$_GET[‘count_price_min‘].""; 11 } 12 13 14 if(!empty($_GET[‘begintime‘])){//下单开始日期搜索 15 $_GET[‘begintime‘]=strtotime($_GET[‘begintime‘]);//将日期转为时间戳 16 $where.= " and $tableName.add_time >=".$_GET[‘begintime‘].""; 17 $_GET[‘begintime‘]=date(‘Y-m-d‘,$_GET[‘begintime‘]);//将日期转为时间戳 18 } 19 20 if(!empty($_GET[‘endtime‘])){//下单结束日期搜索 21 $_GET[‘endtime‘]=strtotime($_GET[‘endtime‘]);//将日期转为时间戳 22 $where.= " and $tableName.add_time <=".$_GET[‘endtime‘].""; 23 $_GET[‘endtime‘]=date(‘Y-m-d‘,$_GET[‘endtime‘]);//将时间戳转换成日期,方便刷新页面后前台显示 24 } 25 26 27 if(!empty($_GET[‘finishbegintime‘])){//交易完成开始日期搜索 28 $_GET[‘finishbegintime‘]=strtotime($_GET[‘finishbegintime‘]);//将日期转为时间戳 29 $where.= " and $tableName.ok_time >=".$_GET[‘finishbegintime‘].""; 30 $_GET[‘finishbegintime‘]=date(‘Y-m-d‘,$_GET[‘finishbegintime‘]);//将日期转为时间戳 31 } 32 33 if(!empty($_GET[‘finishendtime‘])){//交易完成结束日期搜索 34 $_GET[‘finishendtime‘]=strtotime($_GET[‘finishendtime‘]);//将日期转为时间戳 35 $where.= " and $tableName.ok_time <=".$_GET[‘finishendtime‘].""; 36 $_GET[‘finishendtime‘]=date(‘Y-m-d‘,$_GET[‘finishendtime‘]);//将时间戳转换成日期,方便刷新页面后前台显示 37 } 38 39 if(!empty($_GET[‘send‘])){//查询已发货预警订单,发货时间距离此刻超过五天 40 $where.= " and $tableName.send_time < ‘".(time()-60*60*24*5)."‘"; 41 } 42 43 if(!empty($_GET[‘doingorder‘])){//查询处理中的订单 44 $where.= " and $tableName.status in (0,1)"; 45 } 46 47 if(!empty($_GET[‘warningorder‘])){//查询预警的订单:已经付款且时间超过24小时未发货 48 $where.= " and $tableName.pay_time < ‘".(time()-60*60*24)."‘"; 49 } 50 51 if(!empty($_GET[‘warningorder‘])){//查询预警的订单:已经付款且时间超过24小时未发货 52 $where.= " and $tableName.is_pay = 1 "; 53 } 54 if(!empty($_GET[‘warningorder‘])){//查询预警的订单:已经付款且时间超过24小时未发货 55 $where.= " and $tableName.status in (0,1)"; 56 } 57 58 if(!empty($_GET[‘count_price_max‘])){//查询订单最大金额 59 $where.= " and $tableName.count_price <=".$_GET[‘count_price_max‘].""; 60 } 61 62 if(!empty($_GET[‘user_nick_name‘])){//查询采购商名称 63 $where.= " and fab_user.nick_name like ‘".$_GET[‘user_nick_name‘]."%‘"; 64 } 65 if(!empty($_GET[‘user_name‘])){//查询采购商账号 66 $where.= " and fab_user.user_name like ‘".$_GET[‘user_name‘]."%‘"; 67 } 68 69 70 if(!empty($_GET[‘supplier_nick_name‘])){//查询供应商商名称 71 $where.= " and fab_supplier.nick_name like ‘".$_GET[‘supplier_nick_name‘]."%‘"; 72 } 73 74 if(!empty($_GET[‘supplier_name‘])){//查询供应商账号 75 $where.= " and fab_supplier.supplier_name like ‘".$_GET[‘supplier_name‘]."%‘"; 76 } 77 78 if($_GET[‘history‘] == 1){ 79 $where .= " and {$tableName}.status in (2,3,4) "; 80 } 81 82 83 if(($_GET[‘pay_type‘])!=""&&($_GET[‘pay_type‘])!=-1){//查询支付方式 84 $where.= " and fab_order_info.pay_type = ".$_GET[‘pay_type‘].""; 85 } 86 87 88 if(($_GET[‘status‘])!=""&&($_GET[‘status‘])!=-1){//查询订单状态 89 $where.= " and fab_order_info.status = ".$_GET[‘status‘].""; 90 } 91 92 93 94 95 96 if(!empty($_GET[‘stime‘]) && !empty($_GET[‘etime‘])){ 97 $stime = strtotime($_GET[‘stime‘]); 98 $etime = strtotime($_GET[‘etime‘]) + 24*60*60; 99 $where.= " and ($tableName.`inputtime` between ‘$stime‘ and ‘$etime‘)"; 100 } 101 $count = $this->where($where)->count(); 102 $this->countNum = $count; 103 $Page = new \Think\Page($count,$pagesize); 104 $this->page = $Page->show(); 105 $limit = $Page->firstRow.‘,‘.$Page->listRows; 106 $sql="select $tableName.*,fab_supplier.nick_name as supplier_nick_name,fab_user.nick_name as user_nick_name 107 from ($tableName left join fab_supplier on fab_order_info.supplier_id=fab_supplier.supplier_id) 108 left join fab_user on fab_order_info.user_id=fab_user.user_id where $where order by $tableName.`order_id` desc limit $limit"; 109 110 111 112 $sqls="select sum(fab_order_info.count_price) as order_price,count(fab_order_info.count_price) as order_count 113 from $tableName where $where order by $tableName.`order_id` desc limit $limit"; 114 115 $this->sql_msg=$this->query($sqls); 116 117 118 return $this->query($sql);//订单详细信息 119 }
你只需要留意那个GET数据获取,然后进行拼接SQL语句;你为何总是拼接错误呢!!!
标签:
原文地址:http://www.cnblogs.com/haveadream435/p/5769079.html