4

I am a bit stumped with handling multiple and/or statements within an if statement. I'm hoping someone can help me out with this. The Velocity reference guide doesn't appear to address what I am trying to do.

I am working with an application in which I will be using public fields. I have to map an ID from a public field with a name. I have two components for my if statement. I have first an OR operand handling two values of a field. I then have another component to where an AND statement has to be combined.

Here is what I currently have:

      #if ($!myid == '21' && $!mytechno == 161)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;John Smith
      #elseif ($!myid == '21' && $!mytechno == 162)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Mickey Mouse
      #elseif ($!myid == '21' && $!mytechno == 164)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Jean Jones)
      #else<B>Technologist:</B>&nbsp;$!MytechData

      #end
      #end

So this is working correctly so far. Basically I am checking the value of both the myID AND making sure that a mytechno has to exist. That way, it will populate that Tech2 field that I am referencing.

While this is all and good, I need to add in another myid value. Let's say that myID value is 10. I tried doing this

    #if ($!myid == '21' || $myid='10' && $!mytechno == 161)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;John Smith
      #elseif ($!myid == '21' || $myid='10' && $!mytechno == 162)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Mickey Mouse
      #elseif ($!myid == '21 || $myid='10'' && $!mytechno == 164)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Jean Jones)
      #else<B>Technologist:</B>&nbsp;$!MytechData

      #end
      #end

What happens in this situation is that it checks the first statement and assumes it to be true since the myID matches the data that I am working with. So, if the mytechno is 164, it displays "John Smith" The logic fails to check the mytechno field.

I also tried this

      #if ($!myid == '21' || $myid='10') && ($!mytechno == 161)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;John Smith
      #elseif ($!myid == '21' || $myid='10') && ($!mytechno == 162)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Mickey Mouse
      #elseif ($!myid == '21 || $myid='10'') && ($!mytechno == 164)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Jean Jones)
      #else<B>Technologist:</B>&nbsp;$!MytechData

      #end
      #end

That didn't work either. Can anyone offer any suggestions? Just to sum it up, it should be an OR statement combined with an AND statement. If this was SQL I could easily do this, just not sure with Velocity

1 Answer 1

7

I think I found this out. So here is what I did

      #if (($!myid == '21' || $myid='10') && ($!mytechno == 161)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;John Smith
      #elseif (($!myid == '21' || $myid='10') && ($!mytechno == 162)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Mickey Mouse
      #elseif (($!myid == '21 || $myid='10'') && ($!mytechno == 164)
      <B>Technologist:</B>&nbsp;$!MytechData<BR><BR>
      <B>Tech2</B>&nbsp;Jean Jones
      #else<B>Technologist:</B>&nbsp;$!MytechData

      #end
      #end

I still need to test further. But it looks like I had to include an extra set of parenthesis.

EDIT: I had to take out that extra parenthesis at the end. Otherwise, the extra parenthesis displayed on the application itself. Not sure why I had to remove it as I thought you had to put an extra set of parenthesis when enclosing data. At least that is how it works with SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.