We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17833
    • 28 Posts
    I thought i had this figured out, but i guess i’m dumber than i thought wink

    i have a snippet that is supposed to return an array. it works fine when i debug it from within the snippet itself, but when i call the snippet from another snippet, i get the string value "Array" instead of an actual array.

    from the first snippet:
    $job=array(’id’=>$row[’id’], ......);
    return $job;

    the second snippet just uses:
    $job=$modx->runSnippet(’getData’,array(’jobid’=>1));
    return $job[’id’];

    the above outputs to display the letter ’A’
    if i change it to ’return $job[0];’ i get the same ’A’
    $job[1] results in ’r’, as does $job[2], and $job[3] and [4] result in ’a’ and ’y’ respectively. Where am i going wrong with this?
      • 22303 MODX Staff
      • 10,725 Posts
      A snippet in MODx returns only content (at least for now), i.e. a string. The better way to approach what you want to achieve in MODx, is to use something like this in the getData snippet:
      <?php
      $modx->setPlaceholder('job', array('id'=>row['id']));
      ?>

      and then in the snippets you want to use that value:
      <?php
      $modx->runSnippet('getData', array('id'=>1));
      $job= $modx->getPlaceholder('job');
      ?>

      to get the value set by the first snippet.

      That, or use a TV with an @SELECT binding.
      ;)
        • 17833
        • 28 Posts
        Thanks for the reply!
        I had worked around the problem using implode() and explode() in the two snippets, but that caused me to lose the keys to the array. Am i correct in assuming that the solution you offered can only keep one value?
        ie: $modx->setPlaceholder(’job’, array(’id’=>row[’id’],’name’=>$row[’name’], ....et cetera)); wouldnt work?

        if this is the case, would i be better off by including my snippet as a class in a file? the example i showed in this thread is one of several snippets, with each one basically handling database access to a specific table.
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: Machiavelli at May 29, 2006, 05:26 PM

          Thanks for the reply!
          I had worked around the problem using implode() and explode() in the two snippets, but that caused me to lose the keys to the array. Am i correct in assuming that the solution you offered can only keep one value?
          ie: $modx->setPlaceholder(’job’, array(’id’=>row[’id’],’name’=>$row[’name’], ....et cetera)); wouldnt work?

          if this is the case, would i be better off by including my snippet as a class in a file? the example i showed in this thread is one of several snippets, with each one basically handling database access to a specific table.

          You are incorrect in your assumption; that would work perfectly. An Array in PHP, which is all that $modx->placeholders is, can store anything you want to store on it, an integer, a string, another Array, or even an Object. So build yourself a whole rowset, or collection of 20 rowsets if you want as an array, and you can pass it around via the placeholder. It’s just a copy of the original variable being stored in an array element.
            • 17833
            • 28 Posts
            cool! thanks again!
              • 1510
              • 2 Posts
              Hello,

              many thanks for that.

              I was looking for this important information yesterday for 6 hours. It is not easy to find by search.
              Could it be placed also here:
              http://modxcms.com/snippets.html
              http://wiki.modxcms.com/index.php/De:Snippets_erstellen
              http://wiki.modxcms.com/index.php/Snippets_Overview
              http://wiki.modxcms.com/index.php/Creating_Snippets