Announcement

Collapse
No announcement yet.

Toolkit Question! (Explode String)

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

    Toolkit Question! (Explode String)

    Our attributes are in the format "Size / Color". I would like to split them apart on the basket screen so that it shows:
    Size: Small
    Color: White

    Since I feel like this is easier to understand. We do this outside of Miva Merchant using PHP and the code is simple (for those that understand PHP):
    Code:
    list($size_prompt, $color_prompt) = explode(' / ', $prod_attributes['options_prompt'], 2);
    The important parts are that explode() will take a string ($prod_attributes['options_prompt']) and split it at the first instance of " / " (the third parameter in that function ensures that it only splits once, regardless of how many times the string exists). The result of that function is piped into list(), which makes two new variables $size_prompt and $color_prompt.

    Now, I've looked through the toolkit functions and I don't think it can explode or use a delimiter for a string. If it can, that would be ideal and we can stop here. If I'm right, the way I was thinking of doing this was using the substring function. If I can find the position of the first occurence of " / " in the string, I can then use substring to show the before and after parts. What I can't figure out how to do is find the position of the string. SMT allows me to test and return the position using IN/CIN, but I don't believe I can use that in the toolkit.

    I'm tossing this one out for the toolkit wizards to see if this can be done. If it can't, I 'll be sad, but I'll get over it.

    Thanks!

    #2
    Re: Toolkit Question! (Explode String)

    The TK has a gettoken function. The delimiter is the / so you first get the 1 part and then the next line get the 2 part. The delimiter separates each part.
    Bill Weiland - Emporium Plus http://www.emporiumplus.com/store.mvc
    Online Documentation http://www.emporiumplus.com/tk3/v3/doc.htm
    Question http://www.emporiumplus.com/mivamodu...vc?Screen=SPTS
    Facebook http://www.facebook.com/EmporiumPlus
    Twitter http://twitter.com/emporiumplus

    Comment


      #3
      Re: Toolkit Question! (Explode String)

      I shouldn't be shocked that there's a function for this, but I always am (not sure how I missed the second function, I swear I looked).

      Can you help me out with the syntax?

      Code:
      <mvt:item name="toolkit" param="gettoken|l.all_settings:option:attr_prompt, / ,2" />
      Doesn't output anything and I'm not sure what the 2 does either, does that return the 2nd match?

      EDIT: Studied the example harder (hard to follow with the weird math and variable names) and I understand the third parameter now. Once I start getting output, I'll have a follow up question....
      Last edited by Brandon MUS; 05-11-11, 02:46 PM.

      Comment


        #4
        Re: Toolkit Question! (Explode String)

        I actually like this syntax better for your scenario.
        <mvt:item name="toolkit" param="gettoken|l.all_settings:option:attr_prompt,/,1|text1" />
        &mvte:global:text1;

        That said, the item may not like the / in the middle of it. The slash in items is used to delineate the end. So if the above does not work, you'll need to use the mvassign function that lets you use the asciichar(?) function instead of the /. Off hand I don't know what the number of that character is. You'd have to look it up in an ascii table and replace the ? with the number. A bit more involved so see if the above works first. Something like
        <mvt:item name="toolkit" param="mveval|gettoken(l.all_settings:option:attr_ prompt,asciichar(?),1)" />
        Last edited by wcw; 05-11-11, 02:48 PM.
        Bill Weiland - Emporium Plus http://www.emporiumplus.com/store.mvc
        Online Documentation http://www.emporiumplus.com/tk3/v3/doc.htm
        Question http://www.emporiumplus.com/mivamodu...vc?Screen=SPTS
        Facebook http://www.facebook.com/EmporiumPlus
        Twitter http://twitter.com/emporiumplus

        Comment


          #5
          Re: Toolkit Question! (Explode String)

          Yeahh, that did it. This works:
          Code:
          <mvt:item name="toolkit" param="mveval|gettoken(l.all_settings:option:attr_prompt,asciichar(47),1)" />
          Now, two more things.
          1) Not critical, but our naming convention is really " / ", not "/". I can't think of any conflicts off the top of my head, but to keep consistent logic, is there a faster way to concat "<space>asciichar(47)<space>"?

          2) This one is a little more critical. When the product is multiple colors, the prompt looks like "Small / Red / White / Blue". The code we have so far will show Size: Small, Color: Red. Is there a way to ignore the slashes after the first match?

          Comment


            #6
            Re: Toolkit Question! (Explode String)

            ' '$asciichar(47)$' '

            I don't know what your data is. But you can use the mvassign to break it up and save to variables instead of the mveval. See the product page examples of the mvassign and mveval.
            Bill Weiland - Emporium Plus http://www.emporiumplus.com/store.mvc
            Online Documentation http://www.emporiumplus.com/tk3/v3/doc.htm
            Question http://www.emporiumplus.com/mivamodu...vc?Screen=SPTS
            Facebook http://www.facebook.com/EmporiumPlus
            Twitter http://twitter.com/emporiumplus

            Comment


              #7
              Re: Toolkit Question! (Explode String)

              Too questions before I try too hard at this.
              1) Can gettoken take a multi-character delimiter? I can't seem to get that to work even with mveval
              Code:
              <mvt:item name="toolkit" param="mveval|gettoken(l.all_settings:option:attr_prompt,' ' $ asciichar(47) $ ' ',1)" />
              2) Can gettoken take a variable as a delimiter? It seems to fail when I try.
              Code:
              <mvt:item name="toolkit" param="gettoken|l.all_settings:option:opt_prompt,g.prompt_delim" />
              I appreciate the help.

              Comment


                #8
                Re: Toolkit Question! (Explode String)

                1) With the mveval and mvassign you can have a multi char, e.g. '#|'

                2) Not with the gettoken function. You have to use the mveval/mvassign. eg asciichar(47) is technically a variable.
                Bill Weiland - Emporium Plus http://www.emporiumplus.com/store.mvc
                Online Documentation http://www.emporiumplus.com/tk3/v3/doc.htm
                Question http://www.emporiumplus.com/mivamodu...vc?Screen=SPTS
                Facebook http://www.facebook.com/EmporiumPlus
                Twitter http://twitter.com/emporiumplus

                Comment


                  #9
                  Re: Toolkit Question! (Explode String)

                  Okay, with a fresh mind this morning, here's what I think I've discovered.

                  So I set up a variable:
                  Code:
                  <mvt:item name="toolkit" param="mvassign|prompt_delim|' ' $ asciichar(47) $ ' '" />
                  Then I try to display the two halves:
                  Code:
                  Size: <mvt:item name="toolkit" param="mveval|gettoken(l.all_settings:option:attr_prompt,g.prompt_delim,1)" /><br />
                  Color: <mvt:item name="toolkit" param="mveval|gettoken(l.all_settings:option:attr_prompt,g.prompt_delim,2)" />
                  However, the size gets displayed and the color does not. Here's my findings. Since I don't have a trailing " / ", I can't get the second half. If I change the prompt_delim to something simple that exists multiple times (like the letter O), changing nothing else, I get two strings displayed.

                  Either way, its not going to affect my code because I think I can going to try and use "substring" to get everything after the first delimiter (since I have size/colors that look like "Small / Red / White / Blue"). Thanks for the help, I think it'll work eventually!

                  Comment


                    #10
                    Re: Toolkit Question! (Explode String)

                    Okay, super, super, super close. Now I'm just seeing that mveval is adding a line break at the end of the return so I get:
                    Code:
                    "Size
                    : Small
                    "
                    instead of
                    Code:
                    "Size: Small"
                    Any way around this? This last tiny hurdle and I'll stop pestering.

                    Comment


                      #11
                      Re: Toolkit Question! (Explode String)

                      Use mvassign instead of mveval. The function to the right is the same. The difference is you assign it to a variable that you can manipulate a bit.
                      Bill Weiland - Emporium Plus http://www.emporiumplus.com/store.mvc
                      Online Documentation http://www.emporiumplus.com/tk3/v3/doc.htm
                      Question http://www.emporiumplus.com/mivamodu...vc?Screen=SPTS
                      Facebook http://www.facebook.com/EmporiumPlus
                      Twitter http://twitter.com/emporiumplus

                      Comment


                        #12
                        Re: Toolkit Question! (Explode String)

                        You sir, are a hero:
                        Code:
                        		<mvt:foreach iterator="option" array="item:options">
                        			<mvt:if expr="l.settings:option:attr_code EQ 'size_color' AND NOT ISNULL l.settings:option:opt_prompt">
                        				<mvt:item name="toolkit" param="mvassign|prompt_delim|' ' $ asciichar(47) $ ' '" />
                        				<mvt:item name="toolkit" param="mvassign|size_attr_prompt|gettoken(l.all_settings:option:attr_prompt,g.prompt_delim,1)" />
                        				<mvt:item name="toolkit" param="mvassign|size_opt_prompt|gettoken(l.all_settings:option:opt_prompt,g.prompt_delim,1)" />
                        				<mvt:item name="toolkit" param="mvassign|color_attr_prompt|substring(l.all_settings:option:attr_prompt,indexof(g.prompt_delim,l.all_settings:option:attr_prompt,1)+len(g.prompt_delim),200)" />
                        				<mvt:item name="toolkit" param="mvassign|color_opt_prompt|substring(l.all_settings:option:opt_prompt,indexof(g.prompt_delim,l.all_settings:option:opt_prompt,1)+len(g.prompt_delim),200)" />
                        
                        &mvt:global:size_attr_prompt;: &mvt:global:size_opt_prompt;<br />
                        &mvt:global:color_attr_prompt;: &mvt:global:color_opt_prompt;<br />
                        			</mvt:if>
                        		</mvt:foreach>
                        Seems to work flawlessly. Thank you so much for walking me through this -- wouldn't have been able to figure it out otherwise

                        Comment

                        Working...
                        X