We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13226
    • 953 Posts
    I found the following snippet code (Original Post) created by opengeek some years back called GetChildCount:

    <?php
    // initialize and get the number of children for a particular parent
    $childCount = 0;
    if ($children = $modx->getChildIds($parent, $depth)) {
        $childCount = count($children);
    }
    // do something with $childCount or just return it
    return $childCount;

    The call:
    [[GetChildCount? &parent=`[[UltimateParent]]` &depth=`1`]]


    When I use the code it counts everything - even if the children are unpublished

    Using the depth setting gives you when using more than "1" a count of all children found in the parent including folders and their children

    Not being a PHPr myself, is it possible to prevent the code from counting unpublished documents (default setting) and adding the function to not count folders at required levels, if yes, how complex would it be to re-write the snippet ?

    Example call:
    [[GetChildCount? &parent=`[[UltimateParent]]` &depth=`2` &hidefolders=`1`]]


    Commercial solutions are welcome.

    Thanks for the help [ed. note: iusemodx last edited this post 8 years, 4 months ago.]
    • You should be able to do something with
      $children = getChildIds ($docID, $depth, array('published' => 1)]);
      return count($children);
        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
        • 13226
        • 953 Posts
        Thanks for your support Susan

        But I have no idea what I shoul do with that nor do I have an idea what it does

        I presume ref: published that the code looks if the document is published or not

        Does this code add the hide folders bit ?
        • No, but that array is like any other array, you can add more conditions. That array sets up the WHERE conditions in the actual query. Let me think about it, I'll see if I can develop the actual snippet for you.
            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
          • Usage:
            [[ShowCount? &docId=`[[UltimateParent]]` &depth=`2`]]

            The snippet code:
            <?php
            // get the snippet propterties
            $docId = $modx->getOption('docId', $scriptPropteries); 
            $depth = $modx->getOption('depth', $scriptProperties);
            // use the getChildIds API function
            $children = $modx->getChildIds($docID, $depth, array('published' => 1, 'isfolder' => 0)]);
            // return the count of the children array
            return count($children);
            

            https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.getchildids
            https://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet
              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
            • It really should have default values for those two getOption functions and a bit of error trapping but this is just a quick-and-dirty snippet for you.
                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
              • Of course there are any number of other ways to do the same thing, this one just comes to mind right now.
                  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
                  • 13226
                  • 953 Posts
                  Thanks Susan, but it doesnt work sad

                  I get the following:
                  Parse error: syntax error, unexpected ']' in MYPATH\cache\includes\elements\modsnippet\30.include.cache.php on line 6

                  # Time Memory Function Location
                  1 0.0006 281760 {main}( ) ..\index.php:0
                  2 0.0937 7797880 modX->handleRequest( ) ..\index.php:69
                  3 0.0950 8015992 modRequest->handleRequest( ) ..\modx.class.php:1404
                  4 0.1438 10792568 modRequest->prepareResponse( ) ..\modrequest.class.php:129
                  5 0.1446 10879888 modResponse->outputContent( ) ..\modrequest.class.php:145
                  6 0.1474 10969168 modResource->process( ) ..\modresponse.class.php:75
                  7 0.1567 11974584 modTemplate->process( ) ..\modresource.class.php:446
                  8 0.1574 11994872 pdoParser->processElementTags( ) ..\modtemplate.class.php:120
                  9 0.1574 11995328 modParser->processElementTags( ) ..\pdoparser.class.php:57
                  10 0.1694 12376192 pdoParser->processTag( ) ..\modparser.class.php:247
                  11 0.1694 12376448 modParser->processTag( ) ..\pdoparser.class.php:254
                  12 0.1801 12392984 modChunk->process( ) ..\modparser.class.php:484
                  13 0.1810 12394768 pdoParser->processElementTags( ) ..\modchunk.class.php:118
                  14 0.1810 12394768 modParser->processElementTags( ) ..\pdoparser.class.php:57
                  15 0.2137 13437000 pdoParser->processTag( ) ..\modparser.class.php:247
                  16 0.2137 13437128 modParser->processTag( ) ..\pdoparser.class.php:254
                  17 0.2193 13462872 modScript->process( ) ..\modparser.class.php:513
                  • $children = $modx->getChildIds($docID, $depth, array('published' => 1, 'isfolder' => 0));

                    that gets rid of that extra ] near the end. [ed. note: sottwell last edited this post 8 years, 4 months ago.]
                      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