Announcement

Collapse
No announcement yet.

Outputting Array elements by index in Store Morph. &mvt:row:cols[g.col]; fails.

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

    Outputting Array elements by index in Store Morph. &mvt:row:cols[g.col]; fails.

    I'm trying to restructure the data from an existing table. I've managed to get the information in an array with this structure l.settings:rows[n]:cols[n]

    However &mvt:row:cols[g.col]; causes a runtime error;
    Array index must be positive integer
    Code:
    <mvt:assign name="g.col" value="2" />
    <mvt:foreach iterator="row" array="rows">
            &mvt:row:cols[1]; | &mvt:row:cols[g.col]; <br>
    </mvt:foreach>
    What's the workaround? I remember we had this discussion before but can't find it.
    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    #2
    its very un-elegant but this should work. You build the variable as a string value and then use miva_variable_value to evaluate that string as a variable.

    Code:
    <mvt:eval expr="miva_variable_value( 'l.settings:row:col[' $ g.col $ ']' )" />
    Long term we have plans to solve this in the template language.
    Last edited by Brennan; 07-01-16, 07:54 AM.
    Brennan Heyde
    VP Product
    Miva, Inc.
    [email protected]
    https://www.miva.com

    Comment


      #3
      Thanks Brennan, I should have remembered that.

      I ended up using Toolbelts eval function, since it understands variables as array indexes, but yea, it would be nice Store Morph did too. e.g. &mvt:variable[index];
      Ray Yates
      "If I have seen further, it is by standing on the shoulders of giants."
      --- Sir Isaac Newton

      Comment


        #4
        Hi, I'm reopening this thread to address a similar problem. In my case, I need to put the index on the left side of the assignment.

        This works:
        Code:
        <mvt:assign name="l.MyArray[123]" value="'XXXXX'" />
        But this doesn't:
        Code:
        <mvt:assign name="l.MyArray[l.MyIndex]" value="'XXXXX'" />
        I've tried a few different ways to write this, but there doesn't seem to be a way to put an index variable in the destination. Is there a trick I'm not aware of?

        Thanks --
        Last edited by Kent Multer; 06-01-21, 06:12 PM.
        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
          You cannot do it like that, you could do something like this though
          Code:
          <mvt:assgin name="l.MyArray_Size" value="miva_array_insert( l.MyArray, 'XXXXX', l.MyIndex )" />
          David Carver
          Miva, Inc. | Software Developer

          Comment


            #6
            Hey, clever idea! But it doesn't quite work for my project. I need to insert elements into an array, and also update some of them, during a loop thru the basket contents. And it's a sparse array, indexed by product ID.

            Unfortunately, miva_array_insert can only do inserts, not updates. If you call it with an index N, and there's already an element N, the existing element is relocated to N + 1, and higher-numbered elements are also moved up. Similarly, when you remove an array element with miva_array_delete, all the higher-numbered elements' indexes are decreased by 1.

            So to update an element, I would have to re-create the entire array every time I update an element, by copying the elements to a new variable and modifying one of them in the process. Between that and the calls to miva_variable_value, this is getting kind of messy ... Looks like the client will need a module for this. Fortunately, the store is already running some custom modules I wrote, so I can just add this feature to one of those.

            Thanks for the idea; it may come in handy in the future.

            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 could use a combination of miva_array_clear and miva_array_insert. miva_array_clear will delete the elements but not collapse the array.
              David Carver
              Miva, Inc. | Software Developer

              Comment


                #8
                Thanks, I didn't know about miva_array_clear. But I think I'm still going with a different solution for this project.
                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