We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22427
    • 793 Posts
    Hi all,
    I have an array $A of id values and convert it to a Json string:
    $jsonString = $modx->toJSON($A);
    The conversion works correctly.

    Then I try to call migxLoopCollection, using this Json string in the where property:
    $params = array (
    	'packageName' => 'foo',
    	'classname' => 'foo',
    	'tpl' => 'myRowTpl',
    	'where' => '{"id:IN":$jsonString}'
    	);
    $output = $modx->runSnippet('migxLoopCollection',$params);
    
    The $output does not respect the filter. Obviously the where property doesn't accept a variable here.

    How can I solve this problem?

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

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I thought IN used a comma-separated list?

      And even if it does accept a JSON string, the whole 'where' value is a JSON string, and all elements of a JSON string must be surrounded by "..." quote marks.

      Maybe something like
      'where' => '{"id:IN":"' . $jsonString . '"}'
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22427
        • 793 Posts
        With a constant Json array ["42","47","48"] the filter
        'where' => '{"id:IN":["42","47","48"]}'
        works as expected.

        But defining the variable
        $jsonString = '["42","47","48"]';
        the following filter does not:
        'where' => '{"id:IN":$jsonString}'
        (That is, all the items are shown.)
        And it's the same with
        'where' => '{"id:IN":"' . $jsonString . '"}'
        • discuss.answer
          • 4172
          • 5,888 Posts
          $jsonString = '["42","47","48"]';
          
          $params = array (
              'packageName' => 'foo',
              'classname' => 'foo',
              'tpl' => 'myRowTpl',
              'where' => '{"id:IN":' . $jsonString . '}'
              );
          $output = $modx->runSnippet('migxLoopCollection',$params);
          


          or

          $where = array('id:IN'=>array('42','47','48'));
          
          $params = array (
              'packageName' => 'foo',
              'classname' => 'foo',
              'tpl' => 'myRowTpl',
              'where' => $modx->toJson($where)
              );
          $output = $modx->runSnippet('migxLoopCollection',$params);

            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 22427
            • 793 Posts
            Thank you again, Bruno! I think I owe you some beer...
            (The first code works like intented - I didn't try the second one.)