<![CDATA[ Help with php to json - MODX Community Forums]]> https://forums.modx.com/thread/?thread=104246 <![CDATA[Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-560687
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.
]]>
sparker Aug 11, 2018, 08:51 PM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-560687
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561076

I should have noticed it.]]>
BobRay Aug 24, 2018, 06:00 PM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561076
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561065
Sparker thanks for pointing it out for me. We started out with [+weight] and I thought that worked
]]>
nuan88 Aug 24, 2018, 11:14 AM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561065
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561051 BobRay Aug 24, 2018, 02:41 AM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561051 <![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561048
FYI the single case square brackets didn't work it did need to be the [[+placeholder]] syntax.

Thanks again
]]>
sparker Aug 23, 2018, 11:52 PM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561048
<![CDATA[Re: Help with php to json (Best Answer)]]> https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561047
Outer Tpl

<p>[[+total_count]] and [[+weight]]</p>
 <ul>
 [[+inner]]
 </ul>


Then in the snippet (pseudocode):

$output = '';
$outerFields = array(
   // outer chunk placeholders
);

$output .= $modx->getChunk('outerTpl', $OuterFields);

$innerOutput = '';

foreach ($item as $item) {
   $innerFields = array(
     // inner chunk placeholders for this item
   );
   $innerOutput .= $modx->getChunk('innerTpl', $innerFields);
}

/* Insert the inner stuff into the middle of the $output by replacing
   the inner tag with the inner output */

$output = str_replace('[[+inner]]', $innerOutput, $output);
return ($output);


In the last line, str_replace() will be much faster than making MODX parse that placeholder tag.

]]>
BobRay Aug 23, 2018, 10:21 PM https://forums.modx.com/thread/104246/help-with-php-to-json?page=2#dis-post-561047
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-561046

I think its like this, but I could be off. You're gonna have two chunks, no extra snippet as you have grabbed all the 'data' or 'items' in the snippet you do have. That's the php and we are just worried about the output.

If the data you are looking for is anywhere in that array of data, you've got it in memory and can output it.

Also watch out, I believe the placeholder for these values is one bracket, [+item], rather than two. Two brackets is Modx shorthand code, for chunks and snippets and other stuff too. [[$ChunkCall]] and [[!UncachedSnippetCall]]

Start with container and drill down

<div class="container">

[[$OuterChunkTpl]]

</div>


OuterChunktpl contains

<p>[+total_count] and [+weight]</p>
 <ul>
 [[$InnerChunkTpl]]
 </ul>


and InnerChunkTpl contains something like

<li>[+defect rate]</li>
      <li>[+]</li>


Now I beleive you can output all the data if you wish, when the snippet is grabbing just what you need and not necessarily everything in the json. That's an area I don't understand well but it looks a bit like this

<li>[+value]</li>


and then all the values in the array will be listed.

Also, the first example of the page under Processing Chunk via the API might be useful for you, its a simple snippet and chunk for output, to get the list of resources

https://docs.modx.com/revolution/2.x/making-sites-with-modx/structuring-your-site/chunks
]]>
nuan88 Aug 23, 2018, 09:58 PM https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-561046
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-561045
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.

]]>
sparker Aug 23, 2018, 08:11 PM https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-561045
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-560747
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.


]]>
BobRay Aug 14, 2018, 06:55 AM https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-560747
<![CDATA[Re: Help with php to json]]> https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-560743
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?

]]>
sparker Aug 14, 2018, 05:46 AM https://forums.modx.com/thread/104246/help-with-php-to-json#dis-post-560743