Announcement

Collapse
No announcement yet.

Array with a variable?

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

    Array with a variable?

    Scratching my head?
    I have this json array:
    Code:
    storeReply:data[ordCount]:items:data[icount]:options:1:value
    ordCount and icount both are good - but the "1" needs to increment... how can I do this? For the life of me I can't figure out a way to make the "1" a variable and not lose the json associated with it.

    Thoughts? Ideas? smack across the head?
    William Gilligan - Orange Marmalade, Inc.
    www.OrangeMarmaladeinc.com

    #2
    It's hard to give a specific answer from such a short description. But did you know about the miva_struct_members() function? It lets you extract the names of an object's members; then you can use the names to reference the specific members. It only does one nesting level. For instance, if you run it on storeReply:data[ordCount]:items:data[icount]:options, it should give you an array of member "names" which are actually numbers: 1, 2, etc.

    To reference one of these members in an expression, you can use miva_variable_value() to create any kind of odd variable name that's needed to read the contents of a variable or object member, such as
    Code:
    <MvASSIGN NAME="l.member" VALUE="{ miva_variable_value('l.storeReply:data[ordCount]:items:data[icount]:options:' $ l.n) }">
    where l.n contains the number of the specific member you want to read.

    To write to one of these members, you put a string expression in the NAME part of an MvASSIGN, e.g.:
    Code:
    <MvASSIGN NAME="{ 'l.storeReply:data[ordCount]:items:data[icount]:options:' $ l.n }" VALUE="{ someExpression }">
    to write to the n'th member.

    The syntax is a little odd, and all the quotes can be a bit confusing. But this is one of Miva Script's nicer features; I use it quite a bit. I've never written an MvASSIGNARRAY; I just use string expressions to build any kind of weird variable name that I need. Once I had a project where I couldn't use arrays, because there were a lot of elements that needed an index of zero. So I used an object with members that had numbers for names, like what you're seeing here. It worked fine.

    Did I answer your question in there somewhere? :^)
    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
      Still don't think this is good - although I may not be seeign it.. let me rephrase..
      Code:
      COUNTeq 1
      while COUNT less than 5
      <MvAssign name="test" value="{ storeReply:data[ordCount]:items:data[icount]:options:COUNT:value }">
      COUNT +!
      /while
      I have json results which I am parsing. The COUNT must increase by 1, but it is not a member or index - simply another level deep in the returned json.

      Its hard to explain, but I already parse all the levels. But this last one is simply incremented and not given any real structure that I can see.

      Doing this does not work... <MvAssign name="test" value="{ 'storeReply:data[ordCount]:items:data[icount]:options:' $ COUNT $ ':value' }">
      since its not longer a variable. And doing this fails because its not a member..
      <MvAssign name="test" value="{ storeReply:data[ordCount]:items:data[icount]:options:[COUNT]:value }">

      So - how do I write this so I can read the json by incrementing that COUNT?
      William Gilligan - Orange Marmalade, Inc.
      www.OrangeMarmaladeinc.com

      Comment


        #4
        Here is an example of the json I need to read:
        Code:
        "options":{"1":{"id":101,"name":"Size","value":"L","valueId":"376"},"2":{"id":114,"name":"color","value":"blue","valueId":"423"}},
        And this reads it:
        Code:
         
         <MvAssign name="test" value="{ storeReply:data[ordCount]:items:data[icount]:options:1:value }"> and   
         <MvAssign name="test" value="{ storeReply:data[ordCount]:items:data[icount]:options:2:value }">
        But how to do an mvwhile so the 1 and 2 are automated?
        William Gilligan - Orange Marmalade, Inc.
        www.OrangeMarmaladeinc.com

        Comment


          #5
          Code:
          <MvFOR COUNT="5" INDEX="l.n">
           <MvEVAL EXPR="{ '<br/> Option ' $ l.n $ ' has value ' $ miva_variable_value('l.storeReply:data[ordCount]:items:data[icount]:options:' $ l.n $ ':value') }">
          </MvFOR>
          -- will cycle thru your JSON object and pull out the option:"N": value members, or at least the ones named 1 through 5. If you need to find out the set of name/numbers that are in use, you can do that with miva_struct_members as I mentioned before.
          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