Announcement

Collapse
No announcement yet.

I'm having a brain fart moment with if then conditionals using NOT IN vs OR

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    I'm having a brain fart moment with if then conditionals using NOT IN vs OR

    I want to do something IF flag isn't set to HOT or SALE. I also thought having it case insensitive would be nice.

    but i'm having a brain fat on using NOT and IN within an if statement.


    Let's say g.thisprod:flag is either empty or contains NEW

    I want to test for HOT or SALE, if those values are in the flag.. then I do not want to do anything.
    But if flag is empty or contains any other value, then I do want to do some stuff.

    this code doesn't work but I Think it should:
    Code:
    <mvt:if expr="NOT(g.thisprod:flag IN '|HOT|SALE|')">
      do something
    </mvt:if>

    this does work. but it's awkward to maintain as I add possible values to flag and it isn't case insensitive, so I don't want to use this.
    Code:
    <mvt:if expr="(g.thisprod:flag NE 'HOT') OR (g.thisprod:flag NE 'SALE')">
      do something
    </mvt:if>
    my understanding is that using the IN is much faster than an x or y .
    Also I want it to be really easy to add things into that list.. so I want to use the listing of values in the conditional.

    Can someone straighten me out on the syntax?

    #2
    If you know that the string will only contain a limited set of values, and none of the values is a substring of any other, then you can use this format:
    Code:
    <mvt:if expr="NOT (g.thisprod:flag CIN 'HOT,SALE,CLOSEOUT,SPECIAL')">
    Since I used CIN instead of IN, this will work with upper or lower case.
    Kent Multer
    Magic Metal Productions
    http://TheMagicM.com
    * Web developer/designer
    * E-commerce and Miva
    * Author, The Official Miva Web Scripting Book -- available on-line:
    http://www.amazon.com/exec/obidos/IS...icmetalproducA

    Comment


      #3
      Code:
      <mvt:if expr="'|'$toupper(g.thisFlag)$'|' IN '|NEW|SALE|HOT|ETC|'">
      is what i use. forcing the value of the variable to one case or another lets you use IN with the match codes. Has you correctly point out CIN is time consuming because ever character has to be matched twice. toCase is extremely fast and removes those extra tests.
      Bruce Golub
      Phosphor Media - "Your Success is our Business"

      Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
      phosphormedia.com

      Comment


        #4
        Originally posted by Bruce - PhosphorMedia View Post
        Code:
        <mvt:if expr="'|'$toupper(g.thisFlag)$'|' IN '|NEW|SALE|HOT|ETC|'">
        I do use that format in cases where I want to recognize a large number of possible values, without having to make sure that none of them is a substring of any other. I'm not sure that the difference in speed between IN and CIN is enough to justify the extra call to toupper(). For simple operations like that, the VM overhead might be more than the time required to actually operate on the data. Also, the shorter form has the advantage of being easier to read and edit, with fewer punctuation marks and quotes-within-quotes. Sometimes I'm willing to sacrifice a little server CPU time to reduce the workload on my brain :) .
        Kent Multer
        Magic Metal Productions
        http://TheMagicM.com
        * Web developer/designer
        * E-commerce and Miva
        * Author, The Official Miva Web Scripting Book -- available on-line:
        http://www.amazon.com/exec/obidos/IS...icmetalproducA

        Comment


          #5
          Kayak, I'm sure you have this working already but for completeness...

          Place this at the top of your template to make future updates easier.
          Code:
          <mvt:assign name="g.flag_list" value="'|NEW|SALE|HOT|ETC|'" />
          Option 1: Using NOT
          Code:
          <mvt:if expr="NOT ('|' $ toupper(g.thisFlag) $ '|' IN g.flag_list)">
              <mvt:comment> NOT in the list </mvt:comment>
          <mvt:else>
              <mvt:comment> IS in the list </mvt:comment>
          </mvt:if>
          Option 2: Without using NOT (just swap the inner contents)
          Code:
          <mvt:if expr="'|' $ toupper(g.thisFlag) $ '|' IN g.flag_list">
              <mvt:comment> IS in the list </mvt:comment>
          <mvt:else>
              <mvt:comment> NOT in the list </mvt:comment>
          </mvt:if>
          Ray Yates
          "If I have seen further, it is by standing on the shoulders of giants."
          --- Sir Isaac Newton

          Comment


            #6
            Thank you everyone! :-)

            Comment

            Working...
            X