huowz 发表于 2014-05-10 22:26

PHP如何处理jqGrid发过来的复合查询Json条件?

官方demo里只有一个$where变量,可怎么组合成这个串没有讲。现在需要把传过来的json串
filters:

{"groupOp":"OR","rules":[{"field":"area","op":"eq","data":"石家庄"},{"field":"vlanid","op":"eq","data":"4023"},{"field":"vlanid","op":"eq","data":"4024"}],"groups":[{"groupOp":"AND","rules":[{"field":"area","op":"eq","data":"石家庄"},{"field":"vlanyewu","op":"eq","data":" "}],"groups":[]}]}

转化为:
((area = "石家庄" AND vlanyewu = " ") OR area = "石家庄" OR vlanid = "4023" OR vlanid = "4024")
这样就可以放到查询条件里。

huowz 发表于 2014-05-11 14:21

本帖最后由 huowz 于 2014-05-11 14:30 编辑

已解决!参考这里:http://bbs.phpchina.com/forum.php?mod=viewthread&tid=192803&page=1#pid1614618。
美中不足就是还不能处理带括号的情况,继续研究。
   function _makeCondition($field, $oper, $string){
            $condition="";
            switch ($oper){
               case "eq":
                  $condition=$field . " " . "=" . " " . "'" . $string . "'";
               break;
               case "ne":
                  $condition=$field . " " . "<>" . " " . "'" . $string . "'";
               break;
               case "lt":
                  $condition=$field . " " . "<" . " " . "'" . $string . "'";
               break;
               case "le":
                  $condition=$field . " " . "<=" . " " . "'" . $string . "'";
               break;
               case "gt":
                  $condition=$field . " " . ">" . " " . "'" . $string . "'";
               break;
               case "ge":
                  $condition=$field . " " . ">=" . " " . "'" . $string . "'";
               break;         
               case "bw":
                  $condition="$field"." ". "like ". "'".$string ."%'";
                  break;
               case "ew":
                  $condition="$field"." ". "like "."'%". $string . "'";
                  break;
               case "cn":
                   $condition="$field"." ". "like "."'%". $string ."%'";
                   break;
                  default:
                  $condition=$field . " " . $oper . " " . "'" . $string . "'";
                  break;
            }
      return $condition;      
    }

         $filters = json_decode($_REQUEST['filters'], true);
         $operation = $filters['groupOp'];      
         $rules = $filters['rules'];
         print_r($rules);
         foreach ($rules as $rule) {
            $field=$rule['field'];
            $op=$rule['op'];
            $data=iconv('utf-8', 'gb2312', $rule['data']);
            $condition=_makeCondition($field,$op,$data);
            if($where){
                $where=$where.' '.$operation.' '.$condition;
            }else{
                $where=$condition;
            }//end $rules if      
         }//end $rules foreach

arserangel 发表于 2014-05-23 19:38

8错啊,楼主...
页: [1]
查看完整版本: PHP如何处理jqGrid发过来的复合查询Json条件?