Announcement
Collapse
No announcement yet.
Outputting Array elements by index in Store Morph. &mvt:row:cols[g.col]; fails.
Collapse
X
-
Thanks, I didn't know about miva_array_clear. But I think I'm still going with a different solution for this project.
-
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:
-
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:
-
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:
-
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'" />
Code:<mvt:assign name="l.MyArray[l.MyIndex]" value="'XXXXX'" />
Thanks --Last edited by Kent Multer; 06-01-21, 06:12 PM.
Leave a comment:
-
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:
-
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 $ ']' )" />
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 integerCode:<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>
Tags: None
Leave a comment: