Hello to you Stworthy,
Thanks for an example and It's great to makes me more understanding its concept.
Part of an example :
$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
// $filterRules contains [{\"field\":\"productid\",\"op\":\"contains\",\"value\":\"gg\"},{\"field\":\"attr1\",\"op\":\"contains\",\"value\":\"dd\"}]
Its format is quite strange because in every paramaters included "\" character at the end makes json_decode not works.
So to solve it, I try to clean-up all the "\" character as shown below :
$filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
$cond = "productid like '%%'";
if (!empty($filterRules)){
$filterRules = str_replace('\\','',$filterRules);
$filterRules =json_decode($filterRules);
foreach($filterRules as $rule){
$rule = get_object_vars($rule);
$field = $rule['field'];
$op = $rule['op'];
$value = $rule['value'];
if (!empty($value)){
if ($op == 'contains'){
$cond .= " and ($field like '%$value%')";
} else if ($op == 'greater'){
$cond .= " and $field>$value";
} else if($op == 'equal'){
$cond .= " and $field='$value'";
}
}
}
}
My questions is, it is the proper way to clean-up all the "\" or buggy?
Please advice and show me by code
Regards,
Steven