Announcement

Collapse
No announcement yet.

rendering contents of multi-dimensional array structure

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

    rendering contents of multi-dimensional array structure

    I wasn't planning this, and it turns out to be easy to create this enigma. I've created the array structure in a custom module -- it solved a complex dataset. Now I need to render in a page template. I'm using SMT code here. but I may need to do it in Mivascript too. The issue is I don't know the "max" values so I can stop the loop.

    This illustrates what I need:
    Code:
    <mvt:assign name="g.totalouter" value="miva_array_max(g.data:set[l.settings:outercounter][l.settings:innercounter]:element) " />
    <mvt:assign name="g.totalinner" value="miva_array_max(g.data:set[l.settings:outercounter][l.settings:innercounter]:element) " />
    I won't know the max of total outer and total inner elements:
    Code:
    <mvt:assign name="l.settings:innercounter" value="1" />
    <mvt:assign name="l.settings:outercounter" value="1" />
    <mvt:comment> find the value of g.totalouter </mvt:comment>
    <mvt:while expr="l.settings:outercounter LE g.totalouter">
       <mvt:comment> find the value of g.totalinner </mvt:comment>
       <mvt:while expr="l.settings:innercounter LE g.totalinner">
          <mvt:eval expr = "g.data:set[l.settings:outercounter][l.settings:innercounter]:element" />
          <mvt:assign name="l.settings:innercounter" value="l.settings:innercounter + 1" />
       </mvt:while>
       <mvt:assign name="l.settings:outercounter" value="l.settings:outercounter + 1" />
    </mvt:while>
    Maybe the right question, how do I flatten the outer index of g.data.set to get the max array value? Then in the inside loop, flatten the inner index to get the array max?
    There is a way to accomplish this, right? Might be simple, but I don't see it.

    Thanks,

    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!!

    #2
    I've written some modules that work with multi-dimensional arrays, e.g for managing variants for products that have multiple attributes. But I must admit that I don't understand what you're asking for here. Maybe I'm just slow today, or maybe you need to fill in some details.

    Are the ":element" members arrays? If not, why are you using miva_array_max on them?

    Can you use MvFOREACH instead of MvWHILE? Then you don't have to know in advance how many elements an array has.
    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
      There was a reason I asked the question that way. The client has many pages of template code that use MVT:While to loop through this array structure. It's more intense than I've illustrated. Moving to Foreach might be unavoidable but it isn't what the client wants to see. The miva_array_max statements were pseudo code. Apologies for not being specific, but since the variables could never work I thought the illustration was enough. I guess I was hoping for an undocumented function that could spit out the stats.

      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


        #4
        I guess I don't understand your pseudo-code. Are you trying to look at an array of arrays, and find the largest number of elements for any of the inner arrays?
        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
          There were simply illustrating the need to get the max elements for the outer array and the max element for each of the inners. IOW if they were rows and columns row 1, 4 row 2, 3, row 3, 1, etc.
          So yes, each of the inner arrays. Adding to my fun, each of the inners potentially have members and arrays.

          This isn't a perfect example but does depict the index scenario:
          Code:
          set[1][1]:attr:options[1]:cost=0.01
          set[1][1]:attr:options[1]:type=A
          
          set[1][2]:attr:options[1]:cost=0.02
          set[1][2]:attr:options[1]:type=B
          
          set[2][1]:attr:options[1]:cost=0.03
          set[2][1]:attr:options[1]:type=C
          set[2][1]:attr:options[2]:cost=0.04
          set[2][1]:attr:options[2]:type=D
          The Foreach scenario I prototyped on my actual data almost worked, but doesn't look like it will be what we want either.

          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


            #6
            For the outer array, one call to miva_array max() will give you the number of elements it contains. But miva_array_elements() is a better choice if there's any chance that the outer array is not indexed by consecutive numbers (i.e. a sparse array).

            For the inner arrays, you'll need to use a loop that calls miva_array_max (or miva_array elements) on each one, and save the largest value, something like:
            Code:
            <MvFOREACH ARRAY="l.outer" ITERATOR="l.inner">
              <MvASSIGN NAME="l.innercount" VALUE="{ miva_array_elements(l.inner) }">
              <MvIF EXPR="{ l.innercount GT l.maxcount }">
                <MvASSIGN NAME="l.maxcount" VALUE="{ l.innercount }">
              </MvIF>
            </MvFOREACH>
            After running this, l.maxcount should contain the number of elements in the largest inner array. -- Not tested, but it should be pretty close.

            I don't know of a built-in function, documented or otherwise, that will do this
            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


              #7
              You can use miva_array_collapse in place of miva_array_max to rewrite non-sequential indices before returning the number of elements.
              Gordon Currie
              Phosphor Media - "Your Success is our Business"

              Improve Your Customer Service | Get MORE Customers | Edit Any Document Easily | Free Modules | Follow Us on Facebook
              phosphormedia.com

              Comment

              Working...
              X