We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54063
    • 14 Posts
    Hi,

    I'm getting a response from an API which is working fine and I'm able to set a placeholder for the top level of the object. What I'm struggling with though is how I go about setting the chunk placeholders for the various levels and to loop through for each. Hopefully the code will make more sense than what I'm making...

    I'm returning a response with this:

    		$data = json_decode($result,true);
    
    		if (!empty($data)) {
    		    
    		    $array = $data['data'];
    		    return $modx->getChunk($tplChunk, $array);
    
    		} else {
    			$modx->log(modX::LOG_LEVEL_ERROR, 'failed');
    		}
    


    From this code in my chunk I can then set a [[+weight]] placeholder which works fine, this exists at the top level of the 'data'. However I'm unable to set placeholders for [[+name]] or anything else.

    {  
       "success":1,
       "error":[  
    
       ],
       "data":{  
          "weight":"0.00kg",
          "items":[  
             {  
                "key":"3",
                "name":"Test #1",
                "points":0,
                "product_id":"50",
                "model":"TP1",
                "option":[  
    
                ],
                "quantity":"1",
                "reward":"yes"
             },
             {  
                "key":"4",
                "name":"Test #5",
                "points":0,
                "product_id":"52",
                "model":"TP5",
                "option":[  
    
                ],
                "quantity":"1",
                "reward":"yes"
             }
          ],
          "vouchers":[  
    
          ],
          "coupon_status":"1",
          "coupon":"",
          "voucher_status":"1",
          "voucher":"",
          "reward_status":false,
          "reward":"",
          "sums":[  
             {  
                "title":"Sum Total",
                "text":"Value 2",
                "value":"Value 3"
             }
          ],
          "total_count":1
       }
    }
    


    If anyone can point me in the right direction that would be a huge help. Thanks!

    #noob.

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      Try this (untested):

      TPL chunk (add your own HTML):

      <div class="item">
          weight: [[+weight]]
          key:  [[+key]]
          name: [[+name]]
          etc.
      </div>
      



      Code:

      $fields = array();
      $output = '';
      
      foreach($data['items'] as $item) {
          foreach($item as $key => $value) {
              $fields['weight'] = $data['weight'];
              $fields[$key] = $value;
              $output .= $modx->getChunk('myChunk', $fields);
          }
      }
      
      return $output;


      I had to do this in a hurry, so it may not be right.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 54063
        • 14 Posts
        Thank you! I appreciate the help.

        I couldn't get the code to work from the example above but made a slight edit to get an output and I think it's working ok now.

        $fields = array();
                    $output = '';
                     
                    foreach($data['data']['items'] as $item) {
                         
                       foreach($item as $key => $value) {
                            $fields[$key] = $value;
                        }
                        
                    }
                    
                    $output .= $modx->getChunk('myChunk', $fields);
                    return $output;


        However this only returns the items to the placeholders, if I want to also return the top level data such as total, coupon, reward etc would I need to create a different for each statement? If so how I would go about returning two sets of data into two different chunks?

        Thanks again
          • 46886
          • 1,154 Posts
          well, it seems you've got all the
          [data]
          , right? So you need those values in your template for outputting

          Unless I mistake the relationship somehow
            • 54063
            • 14 Posts
            Thank you for the reply.

            The code I have above just deals with the items within the data object which will hold multiple items. I need another chunk to be a wrapper and contain the top level data such as the total, coupon, reward data and so on... just not sure how to go about doing that.
              • 46886
              • 1,154 Posts
              ah right but that data is in the json file right, has to be I guess.

              Seems to me you would need to modify this part, unless the first part doesn't grab the top level data at all
              foreach($data['items'] as $item) {
                  foreach($item as $key => $value) {
              ...
                      $output .= $modx->getChunk('myChunk', $fields);


              it would be great if there was a term so it could be foreach($TLdata instead
                • 3749
                • 24,544 Posts
                Can you give an example of the output you want (just the first two or three items)
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 54063
                  • 14 Posts
                  Sorry i'm probably not giving the best description of what I'm trying to achieve here.

                  I need items to be in one chunk as there could be many items so I need one chunk for that to repeat it. I then need a second wrapper chunk to output all of the other information. Do I need to create two different snippets here to retrieve what is essentially the same data? Or is there a way I can set items to be in one chunk, and everything else in another chunk and then return everything with just the one snippet?

                    • 3749
                    • 24,544 Posts
                    You should need just one snippet, but you might need two Tpl chunks (inner and outer).

                    The key is to put a placeholder for the inner section in the outer Tpl chunk. Something like this:

                    Outer:

                    // stuff you want at the top here
                       <div class="outer>
                        [[+inner]] // where the list of items goes
                       </div>
                     
                    // stuff you want at the bottom here.
                    


                    The inner Tpl chunk contains the HTML code (with placeholders) for displaying just one of the repeated items. It will be re-used for each item inside the foreach loop, adding each one to the output as it goes. When the loop is finished, you'll use the output to replace the inner placeholder above.

                    If you can post the HTML code you want to display (with some sample data filled in), we could be a lot more help. If you re-post the JSON in the same message, it will make things easier for us.


                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 54063
                      • 14 Posts
                      Thanks for the reply sorry I didn't get a notification that anyone had replied.

                      I think what you're suggesting makes sense I'm not sure how to go about returning it..

                      I have this currently, as you suggested.

                      $fields = array();
                                  $output = '';
                                    
                                  foreach($data['data']['items'] as $item) {
                                        
                                     foreach($item as $key => $value) {
                                          $fields[$key] = $value;
                                      }
                                       
                                  }
                                   
                                  $output .= $modx->getChunk('myChunk', $fields);
                                  return $output;


                      In order to set the other placeholders that exist outside of the items object would I need to create another snippet and call that within my chunk? So for example...

                      Wrapper Chunk

                      <div class="container">
                       [[+total_count]] and [[+weight]]
                       <ul>
                       [[!innerSnippet &tpl=`myInnerTpl`]]
                       </ul>
                      </div>


                      Or is there a better way to do this as I'm already accessing all of the data in the first snippet? Your suggestion of using a placeholder instead of another snippet sounds ideal, but can I use two return statements in the snippet? Sorry if its a really basic question, I'm still learning PHP.