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

    I’m new to the MODx API and not a PHP guru. I’m lost with how to use some of the API return values.
    For instance getParent() returns an array. I hoped some numeric value (parent id) so I could loop as long as the parent id is not 0 and this way find all the nodes that are at the first level of the document tree.

    How to get the identifier of one parent node ?

    Thanks for any help.

    function GetLanguageNodeId($docId, $languages) {
       global $modx;
    
      // (...)
    
       $parentId = $modx->getParent($docId,1,'id');
       echo "parentId : ".$parentId;  // prints 'Array'
    
      // (...)
    
    }
      • 7231
      • 4,205 Posts
      The way to debug it when that happens is to print_r() the array and see the values. In most of the cases the returned array is an associative array so you need the identifier. In your example you would use $parentId[’id’] to get the returned value.

      Also better to use return instead of echo, both work but you will not always want to use echo (depending on the output) and return will always work. So it is better to get in the habit wink
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 15001
        • 697 Posts
        Thanks a lot. kiss
        print_r(); is really the function that I needed to display the content of arrays
        and $parentId[’id’] gives the returned value.

        Many many thanks, this will help me a lot going forward.
          • 15001
          • 697 Posts
          $parentId = $modx->getParent($docId,1,'id');

          (...) getParent() returns an array.
          (...) you would use $parentId[’id’] to get the returned value.
          The same way, if I want to list the IDs of a node’s published children documents in a vector, the value actually returned by getActiveChildren is a 2D array.

          Then, to list the children IDs in a vector, I have to do this.
          // Get available published and undeleted language nodes, sorted by menu index in ascending order.
          $languagesById = $modx->getActiveChildren($root, 'menuindex', 'ASC','id'); // Get language IDs.
          $i=-1;
          foreach ($languagesById as $languageId) {
              ++$i;
              $languagesById[$i] = $languagesById[$i]['id'];
          }
          print_r($languagesById);

          Is there no easier way ?