Announcement

Collapse
No announcement yet.

Conditional based on partial product code

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

    Conditional based on partial product code

    I need to create a conditional that looks for product codes that all start with the same set of characters, for example, CUSTOM

    I've tried digging through code samples but I'm not quite finding what I need.

    Leslie Kirk
    Miva Certified Developer
    Miva Merchant Specialist since 1997
    Previously of Webs Your Way
    (aka Leslie Nord leslienord)

    Email me: [email protected]
    www.lesliekirk.com

    Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

    #2
    http://www.mivascript.com/item/strings/indexof.html does that help?
    Colin Puttick
    Miva Web Developer @ Glendale Designs

    Comment


      #3
      Originally posted by dreamingdigital View Post
      Thanks Colin, that's information overload to me.
      Leslie Kirk
      Miva Certified Developer
      Miva Merchant Specialist since 1997
      Previously of Webs Your Way
      (aka Leslie Nord leslienord)

      Email me: [email protected]
      www.lesliekirk.com

      Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

      Comment


        #4
        The technique depends on what you are actually looking for. If, it's your example, then this works

        <mvt:assign name="g.checkThis" value="tolower(substring(l.settings:product:code, 1, 7))" />
        <mvt:if expr="g.checkThis EQ 'custom'">

        yes, this product code starts with custom

        </mvt:if>

        (Note, I always condition (tolower) inputs that are human entered because you cannot assume humans will be consistent and product codes are not case sensitive. Using tolower() is faster than having miva use CIN, and EQ again is faster than either CIN or IN).

        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


          #5
          Originally posted by Bruce - PhosphorMedia View Post
          Using tolower() is faster than having miva use CIN, and EQ again is faster than either CIN or IN).
          Excuse me for going off-topic, but as an old-school programmer*, I'm always intrigued by performance talk. Where did you learn that tolower() is faster? That solution uses substring(), so there are two function calls. I would have handled this case with
          Code:
          <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
          -- which does use CIN, but has zero function calls instead of 2

          Thanks -- Kent

          * I just realized: I wrote my first code in high school, in 1970. I'm only about a year from the 50th anniversary! I still have a paper-tape copy of one of the programs I wrote. I wonder if I can get an old Teletype 33 somewhere ... :^)
          Last edited by Kent Multer; 12-11-18, 10:15 AM. Reason: error on date
          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


            #6
            Not sure there was one place. But I certainly recall some of this is from work Ivo did waaayyy back in the day.

            I'd think the problem with <mvt:if expr="('custom' CIN g.myVariable) EQ 1"> would be that it evaluates true for custom_product_a and product_customs_of_my_people. (I.e., its not tight enough).

            CIN is definitely slower than IN as it has to test ever character twice.

            But yea, it'd be great to have a 'compiled' resource of validated 'best practices'. At least that way we see less of things like:

            <mvt:if expr="thisVar EQ 'something'">
            <mvt:else>
            Here's where we do things
            </mvt:if>

            :)


            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


              #7
              I'm pretty sure that CIN is not a Boolean operator; it returns a number representing the position of the substring. So it will only return 1 if the specified substring appears at the start of the larger string.
              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


                #8
                LOL, according to a certain developer's book on page 35, you would be correct Kent.

                Scott
                Need to offer Shipping Insurance?
                Interactive Design Solutions https://www.myids.net
                MivaMerchant Business Partner | Certified MivaMerchant Web Developer
                Competitive Rates, Custom Modules and Integrations, Store Integration
                AutoBaskets|Advanced Waitlist Integration|Ask about Shipping Insurance Integration
                My T-shirt Collection is mostly MivaCon T-shirts!!

                Comment


                  #9
                  Originally posted by Kent Multer View Post
                  I'm pretty sure that CIN is not a Boolean operator; it returns a number representing the position of the substring. So it will only return 1 if the specified substring appears at the start of the larger string.
                  Yea, that's right forgot about that...still think CINs are pretty slow...but unless we are talking about huge amount of code, not worth fretting over either way.
                  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


                    #10
                    CIN may be slower than IN, but the difference is internal to the VM. I suspect that it's less than the overhead for two function calls instead of one ... but excuse me for nit-picking, I learned to program back in the days when memory was measured in kilobytes, and clock speeds in MHz. My college buddies and I spent every lunch and dinner talking about that kind of stuff. Old habits die hard :^) .
                    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


                      #11
                      Originally posted by Kent Multer View Post

                      Excuse me for going off-topic, but as an old-school programmer*, I'm always intrigued by performance talk. Where did you learn that tolower() is faster? That solution uses substring(), so there are two function calls. I would have handled this case with
                      Code:
                      <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
                      -- which does use CIN, but has zero function calls instead of 2

                      Thanks -- Kent
                      Not off topic - this help me and probably many, many others learn.
                      Leslie Kirk
                      Miva Certified Developer
                      Miva Merchant Specialist since 1997
                      Previously of Webs Your Way
                      (aka Leslie Nord leslienord)

                      Email me: [email protected]
                      www.lesliekirk.com

                      Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

                      Comment


                        #12
                        Okay, so now back to my original question

                        if I use this

                        Code:
                         
                         <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
                        is there a mvt:assign that goes before this?

                        I tried this and didn't get any results

                        Code:
                        <mvt:assign name="g.checkThis" value="tolower(substring(l.settings:product:code, 1, 7))" />
                        <mvt:if expr="g.checkThis EQ 'custom'">
                        
                        yes, this product code starts with custom
                        
                        </mvt:if>
                        Leslie Kirk
                        Miva Certified Developer
                        Miva Merchant Specialist since 1997
                        Previously of Webs Your Way
                        (aka Leslie Nord leslienord)

                        Email me: [email protected]
                        www.lesliekirk.com

                        Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

                        Comment


                          #13
                          Hmmm, that code should work correctly, even if it takes a few extra nanoseconds :^) . But 'custom' is 6 characters long, not 7.
                          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


                            #14
                            So while goofing around I came up with this

                            Code:
                            <mvt:if expr="'custom' CIN l.settings:product:code">
                            It works. I don't want to take the "it works for me attitude", I do want to use correctly formatted code, so should I not use my snippet?
                            Leslie Kirk
                            Miva Certified Developer
                            Miva Merchant Specialist since 1997
                            Previously of Webs Your Way
                            (aka Leslie Nord leslienord)

                            Email me: [email protected]
                            www.lesliekirk.com

                            Follow me: Twitter | Facebook | FourSquare | Pinterest | Flickr

                            Comment


                              #15
                              That will find products that contain "custom" anywhere in their code, not just at the beginning. If you only want to find it at the beginning of the code, add the parentheses and "EQ 1" as in my earlier example.
                              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

                              Working...
                              X