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

  • Kent Multer
    replied
    Thanks, I didn't know about miva_array_clear. But I think I'm still going with a different solution for this project.

    Leave a comment:


  • dcarver
    replied
    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.

    Leave a comment:


  • Kent Multer
    replied
    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.

    Leave a comment:


  • dcarver
    replied
    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 )" />

    Leave a comment:


  • Kent Multer
    replied
    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.

    Leave a comment:


  • RayYates
    replied
    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];

    Leave a comment:


  • Brennan
    replied
    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.

    Leave a comment:


  • 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.
Working...
X