tp5获取查询条件

108 阅读1分钟
<?php
 
class Base extends Controller
{
 

	/*自动加载显示页面的表格*/
	$show_field=[
		"字段名"=>"字段标题"
	];
	
 
	public function where(sym)
    {
		switch ($sym) {
                case '=':
                case '<>':
                    $where[] = [$k, $sym, (string)$v];
                    break;
                case 'LIKE':
                case 'NOT LIKE':
                case 'LIKE %...%':
                case 'NOT LIKE %...%':
                    $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
                    break;
                case '>':
                case '>=':
                case '<':
                case '<=':
                    $where[] = [$k, $sym, intval($v)];
                    break;
                case 'FINDIN':
                case 'FINDINSET':
                case 'FIND_IN_SET':
                    $where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")";
                    break;
                case 'IN':
                case 'IN(...)':
                case 'NOT IN':
                case 'NOT IN(...)':
                    $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
                    break;
                case 'BETWEEN':
                case 'NOT BETWEEN':
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr)) {
                        continue 2;
                    }
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '') {
                        $sym = $sym == 'BETWEEN' ? '<=' : '>';
                        $arr = $arr[1];
                    } elseif ($arr[1] === '') {
                        $sym = $sym == 'BETWEEN' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $where[] = [$k, $sym, $arr];
                    break;
                case 'RANGE':
                case 'NOT RANGE':
                    $v = str_replace(' - ', ',', $v);
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr)) {
                        continue 2;
                    }
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '') {
                        $sym = $sym == 'RANGE' ? '<=' : '>';
                        $arr = $arr[1];
                    } elseif ($arr[1] === '') {
                        $sym = $sym == 'RANGE' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
                    break;
                case 'LIKE':
                case 'LIKE %...%':
                    $where[] = [$k, 'LIKE', "%{$v}%"];
                    break;
                case 'NULL':
                case 'IS NULL':
                case 'NOT NULL':
                case 'IS NOT NULL':
                    $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
                    break;
                default:
                    break;
            }
        }
		$this->where(); 
	}
	public function Validate()
    {
			/*获取数据并排序*/
		$data = input("post.");
		$datt = ['father_id', 'name', 'type', 'oneself', 'principal', 'mobile', 'site', 'postal_code', 'fax', 'email', 'remark', 'department_id'];
		$datas = SortAnArray($data, $datt);
		 /*定义规则*/
		$rule = [
		  'father_id' => 'require',
		  'name' => 'require' 
		];
		 /*定义提醒*/
		$msg = [
		  'father_id.require' => '上级组织不能为空',
		  'name.require' => '调解组织名称不能为空' 
		];
		/*定义数据*/
		$da = [
		  'father_id' => $datas['father_id'],
		  'name' => $datas['name'] 
		];
		/*逻辑判断*/
			/*定义验证器*/
		$validate = new Validate($rule, $msg);
		/*验证数据*/
		$result = $validate->check($da);
		/*返回验证结果*/
		if (!$result) {
		  return ['10003', __($validate->getError()];
		}else{
			return  [1];
		}
			
	}
	 
	
}