• php 格式化搜索条件规范


    /**
         * 获取搜索条件
         * @param $request 搜索条件
         * @param $condition 已有条件
         */
        public function getSearchCondStr($request, $condition = array()) {
            $validRequest = array();
    
            $objMActivity = new MActivity();
    
            $mark_type = isset($request['mark_type']) ? $request['mark_type'] : '';
            if($mark_type) {
                $aids = $objMActivity->getsIdsByCondition(array('mark_type' => $mark_type, 'is_delete' => DActivity::IS_DELETE_FALSE));
    
                if(isset($condition['aid'])) {
                    $tmpAid = is_array($condition['aid']) ? $condition['aid'] : array($condition['aid']);
                    $aids = array_intersect($tmpAid, $aids);
                }
                $condition['aid'] = $aids;
    
                $validRequest['mark_type'] = $request['mark_type'];
            }
    
            // 活动名称模糊搜索
            $activity_name = isset($request['activity_name']) ? $request['activity_name'] : '';
            if ($activity_name){
    
                $con = "`activity_name` LIKE '%{$activity_name}%'";
                if($mark_type) {
                    $con .= " && mark_type='{$mark_type}'";
                }
                $aids = $objMActivity->getsIdsByCondition($con);
                $condition['aid'] = $aids;
    
                $validRequest['activity_name'] = $request['activity_name'];
            }
    
    
            // 用户昵称 模糊搜索
            $nickname= isset($request['nickname']) ? $request['nickname'] : '';
            if ($nickname){
                $objMUser = new MUser();
                $con = "`nickname` LIKE '%{$nickname}%'";
                $uids = $objMUser->getsIdsByCondition($con);
                $condition['uid'] = $uids;
    
                $validRequest['nickname'] = $request['nickname'];
            }
    
            //  渠道名称模糊搜索 + 渠道地区搜索 begin
            $channel_name = isset($request['channel_name']) ? $request['channel_name'] : '';
            $c_province= isset($request['c_pid']) ? $request['c_pid'] : '';
            $c_city= isset($request['c_cid']) ? $request['c_cid'] : '';
            $c_part= isset($request['c_did']) ? $request['c_did'] : '';
            $channel_condition = '1';
            if ($c_province){
                $channel_condition .= " and province = '{$c_province}'";
                $validRequest['c_pid'] = $request['c_pid'];
            }
            if ($c_city){
                $channel_condition .= " and city = '{$c_city}'";
                $validRequest['c_cid'] = $request['c_cid'];
            }
            if ($c_part){
                $channel_condition .= " and country = '{$c_part}'";
                $validRequest['c_did'] = $request['c_did'];
            }
            if (!empty($channel_name)){
                $channel_condition .= " and `channel_name` LIKE '%{$channel_name}%'";
                $validRequest['channel_name'] = $request['channel_name'];
            }
            if($channel_condition != '1') {
                $channel_condition .= " and is_delete=" . DChannel::IS_DELETE_FALSE;
                $ojbMChannel = new MChannel();
                $condition['cid'] = $ojbMChannel->getsIdsByCondition($channel_condition);
            }
            //  渠道名称模糊搜索 + 渠道地区搜索 end
    
            $intFields = array(
                'win_type' => 'win_status',
                'lottery_type' => 'status',
            );
            foreach($intFields as $field => $reqField) {
                if(isset($request[$reqField]) && Verify::naturalNumber($request[$reqField])) {
                    $condition[$field] = $request[$reqField];
    
                    $validRequest[$reqField] = $request[$reqField];
                }
            }
    
            // 数据库真实字段 => 搜索字段
            $normalFields = array(
                'province' => 'pid',
                'city' => 'cid',
                'part' => 'did',
                'aid' => 'aid',
                'teacher_phone' => 'teacher_phone',
                'teacher_email' => 'teacher_email',
                'teacher' => 'teacher',
                'is_use' => 'is_use',
                'is_delete' => 'is_delete',
            );
            foreach($normalFields as $field => $reqField) {
                if(isset($request[$reqField]) && $request[$reqField]) {
                    $condition[$field] = $request[$reqField];
    
                    $validRequest[$reqField] = $request[$reqField];
                }
            }
    
            // 报名起止时间
            $s_create_time = isset($request['s_create_time']) ? $request['s_create_time'] : '';
            $e_create_time = isset($request['e_create_time']) ? $request['e_create_time'] : '';
            if ($s_create_time && $e_create_time){
                $condition[]['create_time'] = SqlHelper::addCompareOperator('>=', strtotime($s_create_time));
                $condition[]['create_time'] = SqlHelper::addCompareOperator('<=', strtotime($e_create_time));
    
                $validRequest['s_create_time'] = $request['s_create_time'];
                $validRequest['e_create_time'] = $request['e_create_time'];
            }
    
            // 宝宝起止年龄
            $baby_age_min= isset($request['baby_age_min']) ? $request['baby_age_min'] : '';
            if ($baby_age_min){
                $condition[]['baby_age'] = SqlHelper::addCompareOperator('>=',$baby_age_min);
            }
            $baby_age_max= isset($request['baby_age_max']) ? $request['baby_age_max'] : '';
            if ($baby_age_max){
                $condition[]['baby_age'] = SqlHelper::addCompareOperator('<=',$baby_age_max);
            }
    
    
            $condition = $this->objDao->parseCondition($condition);
            $condition = str_replace('WHERE','', $condition);
    
            $likeFields = array(
                'works_name' => 'works_name',
                'baby_name' => 'baby_name',
                'school_address' => 'school_address',
                'school_name' => 'school_name',
            );
            foreach($likeFields as $field => $reqField) {
                if(isset($request[$reqField]) && $request[$reqField]) {
                    $condition .= ($condition ? ' && ' : '') . "`{$field}` LIKE '%" . $request[$reqField] . "%'";
    
                    $validRequest[$reqField] = $request[$reqField];
                }
            }
    
            return array($condition, $validRequest);
        }
  • 相关阅读:
    显示当前时间和全班学生成绩求和
    js做计算器
    javascript学习内容--改变样式、取消设置、显示内容、隐藏内容
    javascript学习内容--object.style.display="value" value值为“”none“隐藏”或 "block"显示
    元素示例
    Python网页爬虫(一)
    macbook Android开发环境搭建,真机调试
    Shell 脚本编程笔记(一) Hello Shell
    嵌入式开发(一) Ubuntu12.04下搭建交叉编译环境
    算法学习(二) 全排列问题的非递归算法——模拟堆栈
  • 原文地址:https://www.cnblogs.com/bandbandme/p/12012321.html
Copyright © 2020-2023  润新知