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

    I would like to count the number of documents inside a container and display that number

    Would be nice if ditto could do that but I did not find a way to do that

    Thx for your help
    • Quote from: xpix at May 22, 2008, 07:18 PM

      Hi,

      I would like to count the number of documents inside a container and display that number

      Would be nice if ditto could do that but I did not find a way to do that

      Thx for your help

      You don’t need something as powerful as Ditto to get a simple count. All you need to do is create a simple snippet:
      <?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;
      ?>

      And then call it in your content, e.g. to get just the immediate child count for the current document:
      [!GetChildCount? &parent=`[*id*]` &depth=`1`!]
        • 5340
        • 1,624 Posts
        Yep but if I use ditto to display the a list of containers it would be nice to display the number of documents within Ditto.

        I will have look and see if I can get it to work with ditto

        Thx
        • Quote from: xpix at May 22, 2008, 07:32 PM

          Yep but if I use ditto to display the a list of containers it would be nice to display the number of documents within Ditto.

          I will have look and see if I can get it to work with ditto

          Thx
          Ditto would likely require an extender or something, or the use of ditto placeholders instead of [*id*] for instance. Might post in the Ditto support board for specific help doing things like this most efficiently within Ditto.
            • 29076
            • 615 Posts
            Any luck on this one? I’m looking for the same solution within Ditto. My goal is as table with the names of some containers and the number of documents within those containers.
              I think, thererfor I am! But what I am, and why...?
              • 29076
              • 615 Posts
              Quote from: OpenGeek at May 22, 2008, 07:27 PM

              <?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;
              ?>

              And then call it in your content, e.g. to get just the immediate child count for the current document:
              [!GetChildCount? &parent=`[*id*]` &depth=`1`!]


              Is there a way to NOT count the containers, only plain documents, within these script?
                I think, thererfor I am! But what I am, and why...?
                • 5340
                • 1,624 Posts
                here is my solution

                use the ditto extenders with ChildrenCount


                Here is the extender file(without comments)

                <?php
                $GLOBALS['ditto_object'] = $ditto;
                $placeholders['counterCh'] = array("id","countChildrens");
                	
                
                if (!function_exists("countChildrens")) {
                	
                	function countChildrens($resource) {
                	
                		$lr = '[[ChildrenCount? &parentId=`'.$resource[id].'`]]';
                		return $lr;
                	}
                }
                ?>



                call ditto like this:

                [[Ditto? &parents=`35` &depth=`1` &display=`all` &sortDir=`ASC` &tpl=`buysell` &extenders=`count-childs`]]

                in your ditto template add [+count-childs+]

                Let me know if you have problems
                  • 29076
                  • 615 Posts
                  Thanks for trying, but I’m sorry, it doesn’t work. I followed your instructions, and there are no errors, the script only gives an empty result. Nothing.

                  Here is my Ditto-call: <ul>
                  [[Ditto?parents=`369` &depth=`2` &tpl=`Familiy-list-tpl` &display=`all` &sortBy=`pagetitle` &sortDir=`ASC` &extenders=`count-childs` ]]
                  </ul>

                  And here is my template: <li>[+pagetitle+] - [+count-childs+]</li>

                  Here is the result:
                  <ul>
                  <li><em<Polygonum</em> L. - </li>
                  <li><em>Achillea millefolium</em> - </li>
                  <li><em>Achillea</em> L. - </li>

                  <li><em>Alchemilla glomerulans</em> - </li>
                  <li><em>Alchemilla</em> Bus. - </li>
                  <li><em>Alopecurus magellanicus</em> - </li>
                  <li><em>Alopecurus</em> L. - </li>

                  Mabye I do something wrong.
                    I think, thererfor I am! But what I am, and why...?
                    • 5340
                    • 1,624 Posts
                    Did you set up the extender correctly?

                    1. go to snippets/ditto/extenders and create a new file count-childs.extender.inc.php
                    2. paste the code above

                    also

                    did you install the ChildrenCount snippet?
                      • 5340
                      • 1,624 Posts
                      I made a mistake

                      Use

                      <li>[+pagetitle+] - [+counterCh+]</li>