PHP variable containing conditional statement

84 views Asked by At

I'm using jQuery's Query Builder from here http://querybuilder.js.org/demo.html and my main page is written in PHP. I've used the query builder to basically specify conditions that I would like to use in my conditional statements.

I'm not too far from converting the input into the format (((A == B) && (C != A)) || (D == E)) as an example. For this I've used most of this solution. https://stackoverflow.com/a/26607204/1724376

Now I'm stuck thinking I may have gone down the wrong path as I could append "$" to the variables and potentially use eval() but that's something I really don't want to do even though this will be an internal site.

There are 36 variables so I can't think of any other solution to build the conditional statement either since there could be multiple permutations of what the user wants to (and should be able to) use as the conditions.

I'm afraid the answer will be that eval() is the only choice but as a last ditch effort before I toss out hours of work, I figured I'd ask folks a lot smarter than me.

Guidance will be appreciated.

Edit: My end goal is to allow a jQuery query builder-ish UI to allow the user to specify the exact conditions under which one specific function will be executed. That function will not change, but the conditions may. The user should not need to modify code.

1

There are 1 answers

0
Predator On

After much research, I've only been able to find eval() as a viable approach. I'm sanitizing the input by limiting the size of the variable, comparing it against what I know to be valid options from the select statements, and also verifying that the input doesn't contain anything besides letters and numbers.

I wish there was a better option but this approach seems to work for now.