PDA

View Full Version : Nested if statements


Neocoder
04-14-06, 02:26 PM
I'm creating a page based on the user's input to an html form. There are several options to how the page can look depending on their choices.

I built a page with nested if's that isn't working. Here's a sample that isn't working. When the criteria are met for the two if statements, it doesn't display the error message on line 3, it displays my site's 404 "not found" page. What am I doing wrong?


<MvIF EXPR="{lang1 EQ 'greek' OR 'italian'}">
<MvASSIGN NAME="NT1" VALUE="{Y}">
</MvIF>

<MvIF EXPR="{book1 LT 40}">
<MvIF EXPR="{NT1 EQ 'Y'}">
<p>&nbsp;<br>Sorry, your selection is not available.
<MvELSE>
<MvCALL ACTION="{'http://www.mysite.com/parallel/' $ g.lang1 $ '/' $ book1 $ '_' $ chap1P $ '.htm'}" METHOD="GET">
<MvASSIGN NAME="temp" VALUE="{glosub(callvalue,'<pre>','') }">
<MvEVAL EXPR="{ glosub(temp,'</strong>','</strong><br>') }">
</mvcall>
</MvIF>
<MvELSE>
<MvCALL ACTION="{'http://www.mysite.com/parallel/' $ g.lang1 $ '/' $ book1 $ '_' $ chap1P $ '.htm'}" METHOD="GET">
<MvASSIGN NAME="temp" VALUE="{glosub(callvalue,'<pre>','') }">
<MvEVAL EXPR="{ glosub(temp,'</strong>','</strong><br>') }">
</mvcall>

</MvIF>

aGorilla
04-14-06, 03:53 PM
<MvASSIGN NAME="NT1" VALUE="{Y}">

That's an empty assign, unless you have some variable named 'Y'. Should be:
<MvASSIGN NAME="NT1" VALUE="Y">

Neocoder
04-14-06, 06:36 PM
Thank you. Changing the assign statement helped in that I'm now displaying the error message. But I still have a problem.

I want to display the error message only when both of the If statements are true. But Miva seems to be treating my nested Ifs as an OR, and I'm getting the error message when the first test is True and the second test is False. It never gets to the ELSE.

aGorilla
04-14-06, 06:45 PM
Tough to tell exactly what you're trying to do. Could you pseudocode the scenario?

My take on it is that you have it backwards, here's my _guess_ of what you are trying to do...

If the languag is Greek or Italian, hand them the appropriate page, otherwise, display the 'Sorry, your selection is not available.' message.

Is my guess right?

aGorilla
04-14-06, 06:52 PM
Is my guess right?
Assuming I am right (yes, a dangerous thing to do)...


<MvCOMMENT> lowercase it just in case (pardon the pun) </MvCOMMENT>
<MvASSIGN NAME="g.Lang1" VALUE="{tolower(g.Lang1)}">
<MvCOMMENT> if it's not greek or italian, l.have_page will be '0' (ie: false) </MvCOMMENT>
<MvASSIGN NAME="l.have_page" VALUE="{(g.Lang1 EQ 'greek') OR (g.Lang1 EQ 'italian')}">

<MvIF EXPR="{l.have_page}">
<MvCOMMENT> Yes, I speak that language. </MvCOMMENT>
<MvCALL ACTION="{'http://www.mysite.com/parallel/' $ g.Lang1 $ '/' $ book1 $ '_' $ chap1P $ '.htm'}" METHOD="GET">
<MvASSIGN NAME="temp" VALUE="{glosub(callvalue,'<pre>','') }">
<MvEVAL EXPR="{ glosub(temp,'</strong>','</strong><br>') }">
</MvCALL>
<MvELSE>
<MvCOMMENT> No hablo g.Lang1 </MvCOMMENT>
<p>&nbsp;<br>Sorry, your selection is not available.
</MvIF>

Neocoder
04-14-06, 07:09 PM
I've been able to get it working by making two changes to my MvASSIGN code. I initialized my NT1 variable to "N". And I realized that the variable name was missing after the OR in the EXPR field. With these changes my nested IFs are working fine.

<MvASSIGN NAME="NT1" VALUE="N">
<MvIF EXPR="{lang1 EQ 'greek' OR lang1 EQ 'italian'}">
<MvASSIGN NAME="NT1" VALUE="Y">
</MvIF>

Thanks for your help!

Dramatic
04-15-06, 10:51 AM
Thank you. Changing the assign statement helped in that I'm now displaying the error message. But I still have a problem.

I want to display the error message only when both of the If statements are true. But Miva seems to be treating my nested Ifs as an OR, and I'm getting the error message when the first test is True and the second test is False. It never gets to the ELSE.

It Never gets to the else because
<MvIF EXPR="{lang1 EQ 'greek' OR 'italian'}"> ALWAYS returns true. Miva will evaluate a non-empty string literal as true when used in a logical expression, so 'italian' = TRUE, and you are asking.<MvIF EXPR="{(lang1 EQ 'greek') OR TRUE}">, which is always true.

What you probably meant is: <MvIF EXPR="{(lang1 EQ 'greek') OR (lang1 EQ 'italian')}"> - the parentheses aren't strictly necessary in this case but they help clarify the logic.