Um not sure how to ask this, but I will do my best.
I am working in an “online app” for work, that is a like a dynamic website wysiwyg generator. It let’s me make the pages with html, and then route people around the site using what I think is a mix of Jscript (not javascript) and ASP.
Using their click and point interfact I can make little IFs and WHILEs etc. It is rather groovy actually, and very intuitive.
What I generally want to do with them is to say “if the answer to any of the following questions is 1 or 2 then blahblah”. I have been instructed to do this using the following boolean argument (or request?).
So, if the reply to A, B, or C, is 1 or 2 then FOO looks like this …
I also tried a few random brackets in the second setup, but it didn’t really make a difference. Is there an easier way than the first way (by easier I mean clearer, faster, more concise etc). The only thing I can change is the boolean expression (or request or statement or whatever they are called).
Declaring boolean variable then having a series of if statements would help with clarity. Or, if the only options for the answers are 0, 1, and 2, you could sum the answers, and check if it was > 0.
You’d do better to create a few nested if statements, rather than one single boolean statement. But that’s just because lines that make the editing window scroll aren’t good practice from a software maintenence point of view.
You can’t simplify that using Boolean logic, mainly because it’s just a string of equality tests linked with logical ors. Your second doesn’t work because it doesn’t fit that mold: It doesn’t parse correctly into the tests you want to do.
Forgive me if I am oversimplifying things… I haven’t worked with PHP before.
In the first set of code, you have f(‘q2’)[a’] etc etc, but in the second set you have replaced ‘q2’ with ‘q1’… Is that making a problem for you? (I know there are several times that has happened to me)
If this is in the wrong direction, please provide a little more information, as jbird3000 said too.
LilShieste
IOW, if (q1[a] is true) or (q1** is true) or (q1[c] is equal to 1) or (2 is true) or (3 is true). Which is not what you want at all.
If the answers are really as simple as ‘1’, ‘2’ and ‘3’, then there is likely to be something you can do with > and <, or perhaps some integer math stuff, but it might not be as maintainable as what you have.
I don’t know PHP syntax, but if you can break it up with line feeds and/or add ()s for clarity, I think you’re fine the way you have it.
I’m going to use psuedo-code here since I don’t know the specifics of PHP. Try nested for loops. Use whatever mechanism it has to run thru a range of symbols or items in a set.
bool FoundIt = false
for letter in {'a','b','c'}
for digit in {'1','2','3'}
if f('q2')[letter] == digit
{FoundIt = true}
// test value of FoundIt now
You can throw in suitable loop breaks to break out early if available. (Is ‘q2’ legit in PHP?)
Not exactly “simpler” (or more efficient) code but it works a whole lot better when there is a lot of such cases to try.
There is so much wrong with the OPs 2nd attempt that I can’t stop shaking my head…
It’s not PHP, as I stated in the OP
I just used the “php” tags to make sure it didn’t parse any of the code. Thanks for the suggestions, but unfortunatly the only thing I can change is the part of the code I showed.
Looking at how it tests, LordVor is probably right in how it tried to read the second attempt. If I could get into the “proper” code I can think of much easier ways to get around it, but since it was only that part I can change I am fairly stuck.