We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18830
    • 161 Posts
    This is an auto-generated support/comment thread for ChildrenCount.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    Returns the number of published children of a parent
      • 18830
      • 161 Posts
      I was asked by a member of the MODx-community, if it is possibly to count the children of more than one parent. So I tried to rewrite this snippet. But not being a programmer I need some help:

      Thisis how I started:

      $parentId = explode(",",$parentId);
      
      foreach ($parentId as $value) {
      
        $child = count($modx->getDocumentChildren($value, 1, 0,'id'));
      
      }
      


      $partentId would be an comma seperated list of parents ID´s from the snippet call.

      Now I wanted to have something like

        echo array_sum($child); 


      but $child will not return an array but simply the numbers of the children written one after the other without any delimiter.

      Any ideas?

      Thank you

      Mazso
        • 7455
        • 2,204 Posts
        mabe somthing like this?

        $child = $child + count($modx->getDocumentChildren($value, 1, 0,’id’));

        Dimmy
          follow me on twitter: @dimmy01
          • 18830
          • 161 Posts
          Dimmy, thank you for the fast answer.

          I´m not sure what "child +" will do but that doesn’t seem to work.
            • 7231
            • 4,205 Posts
            If you want child to be an array you need to let it know, it thinks it is a string.

            try adding $child = array() before the foreach loop

            And change the code to
            $child[] = count($modx->getDocumentChildren($value, 1, 0,'id'));


            I´m not sure what "child +" will do but that doesn’t seem to work.
            This should return the total of all the children since it adds the value of each pass in the foreach loop. This would eliminate the array_sum() since you would already have the total value.
              [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]
              • 18830
              • 161 Posts
              Great! Now it works. smiley