Announcement

Collapse
No announcement yet.

documenting different ways to get a comma delimited string into an array

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

    documenting different ways to get a comma delimited string into an array

    Playing around tonight and searching the forums for some help. I thought I'd put all the info in one spot about taking a comma delimited string and putting it into an array. That way, in the future, I will find this info when I'm looking for it ('cause I forget stuff all the time) and it might help someone else too.

    I do have a question too. What is what is the preferred way to do this in Miva 9 and are there more ways or better ways?


    Code:
    <mvt:assign name="l.settings:wcwstuff" value="'some,foo,magic,stuff,makes,a,list,like,this'" />
    
    <BR>
    Array_FromList
    <pre>
     <mvt:item name="ry_toolbelt" param="Array_FromList|l.all_settings:wcwmssgs|l.all_settings:wcwstuff" />
    <mvt:foreach iterator="wcwmssg" array="wcwmssgs">    
            &mvt:wcwmssg;
    </mvt:foreach>
    </pre>
    
    
    <BR>
    miva_array_deserialize with toolbelt assign
    <pre>
    <mvt:item name="ry_toolbelt" param="assign|l.all_settings:wcwmssgs2|miva_array_deserialize(l.all_settings:wcwstuff)" />
    
    
    <mvt:foreach iterator="wcwmssg2" array="wcwmssgs2">  
            &mvt:wcwmssg2;
    </mvt:foreach>
    </pre>
    
    <BR>
    miva_array_deserialize with mvt assign
    <pre>
    <mvt:assign name="l.settings:wcwmssgs2" value="miva_array_deserialize(l.settings:wcwstuff)" />
    <mvt:foreach iterator="wcwmssg2" array="wcwmssgs2">  
            &mvt:wcwmssg2;
    </mvt:foreach>
    
    <BR>
    miva_splitstring
    <pre>
    <mvt:assign name="l.settings:csv" value="'star wars,hi mom!,master yoda,miva docs yay, this example is courtesy of,the,dreamingdigital,pirate'" />
    <mvt:assign name="l.settings:count" value="miva_splitstring( l.settings:csv, ',', l.settings:csv_arr, '' )" />
    &mvt:count; items in that csv var, yo.
    <mvt:foreach iterator="x" array="csv_arr">
    &mvt:x;
    </mvt:foreach>
    
    </pre>
    
    </pre>
    
    <BR>
    do library utlity SplitString
    <pre>
    <mvt:assign name="l.settings:wcwmssgs2" value="miva_array_deserialize(l.settings:wcwstuff)" />
    
    <mvt:do file="g.Module_Library_Utilities" name="l.success" value="SplitString(l.settings:wcwstuff, ',', l.settings:wcwmssgs3)" />
    
    <mvt:foreach iterator="wcwmssg3" array="wcwmssgs3">  
            &mvt:wcwmssg3;
    </mvt:foreach>
    </pre>
    Last edited by kayakbabe; 10-23-19, 08:35 AM.

    #2
    Code:
    <MvASSIGN NAME="l.array" VALUE="{ miva_array_deserialize(l.CommaDelimitedString) }">
    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
      Why are you not using miva_splitstring() function?
      Colin Puttick
      Miva Web Developer @ Glendale Designs

      Comment


        #4
        'cause i don't know how to use it. the documentation is pretty slim. Can you give an example? I'll edit my post and add it.

        Comment


          #5
          <mvt:assign name="l.settings:csv" value="'star wars,hi mom!,master yoda,miva docs yay'" />
          <mvt:assign name="l.settings:count" value="miva_splitstring( l.settings:csv_arr, ',', l.settings:csv, '' )" />
          &mvt:count; items in that csv var, yo.
          <mvt:foreach iterator="x" array="csv_arr">
          &mvt:x;
          </mvt:foreach>
          Colin Puttick
          Miva Web Developer @ Glendale Designs

          Comment


            #6

            1 items in that csv var, yo. ARR! That helped MATEY!
            just a tweek and it works.. i'm putting in above.

            Comment


              #7
              took a guess on that one lol.
              Colin Puttick
              Miva Web Developer @ Glendale Designs

              Comment


                #8
                kayakbabe next you need to figure out how to deal with commas in the comma data - like a CSV Decode, and loop through that without getting messed up by those commas that are within the "" quoted fields. Yup, I'm interested in that.
                Colin Puttick
                Miva Web Developer @ Glendale Designs

                Comment


                  #9
                  I'm admittedly lazy. I would actually delimit with a pipe character probably. glosub "," and replace with | and then split the string on the | . Then commas inside the quoted fields wouldn't matter. Would also have to strip off the preceeding and last " before splitting the string too.

                  That's how I used to handle exports from old merchant before it had real csv export.
                  Last edited by kayakbabe; 10-23-19, 09:28 AM.

                  Comment


                    #10
                    I have to deal with a supplier CSV soon so that's why I'm wondering. And I can't glosub all , with | because the , within the quoted csv fields would get transformed to | and the data breaks. Miva knows how because they let us import proper CSV now, and there is a CSV encode function, but I don't see a CSV decode function. https://docs.miva.com/template-language/read-csv-file that example isn't good enough. I wonder if the LSK has the import functions in it and we can reverse engineer that. Ugg. Why?! :)
                    Colin Puttick
                    Miva Web Developer @ Glendale Designs

                    Comment


                      #11
                      that is a poor example. sure would be nice if they at least gave an example of what the credit.csv contents looked like.

                      Comment


                        #12
                        Are you wanting to automate the product import add/update? that scraping images if they feed you an image url and stuff.. maybe you'd want to pre-process them with php or something like that so you can get the image into your import folder like is done with provisioning.

                        Comment


                          #13
                          yes to previous comment. no to needing php. can't talk details. :P
                          Colin Puttick
                          Miva Web Developer @ Glendale Designs

                          Comment


                            #14
                            You can glosub the delimiting commas with the quotes glosub( g.mystring,'","','|') if they use | in their data.... try using tilde ~ or find some esoteric weird extended ascii character no one uses as your delimiter instead.

                            Comment


                              #15
                              Thanks for documenting this. I had to change a little bit but here is what I ended up with:
                              Code:
                              <mvt:item name="customfields" param="Read_Product_Code( l.settings:product:code, 'bullet_list', l.settings:bullet_list )" />
                              <mvt:assign name="l.settings:csv" value="l.settings:bullet_list" />
                              <mvt:do file="g.Module_Library_Utilities" value="miva_splitstring( l.settings:csv, ',', l.settings:csv_arr, '' )" />
                              <ul>
                              <mvt:foreach iterator="item" array="csv_arr">
                              <li>&mvt:item;</li>
                              </mvt:foreach>
                              </ul>
                              Highly caffeinated
                              http://www.coffeehouseexpress.com

                              Comment

                              Working...
                              X