We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29076
    • 615 Posts
    How to count number of date TV is not empty in a parent resource?

    MODx Revo 2.4.3
    TV called pickeUp, Input Type: Date, Input Option Value: none, Output Type: Date, Date Format: %e.%m.%Y

    For some resources in the parent (in this case the parent container has id=3) I set the pichedUp date, for som I don't, meaning the TV i empty.

    Question: I need to count the number of published, and also total number of resources in a parent resource where the TV called pickedUp is not empty. Can someone Please give me a hint. smiley

    (History: This is a site for Lost and Found, and I set items to "Picked up by owners" by setting the date in the pickedUp TV. And I need to count the number of items that is picked up by owners. For statistical reasons.)

    Thanks. smiley

    This question has been answered by Jako. See the first response.

    [ed. note: Sylvaticus last edited this post 10 years, 6 months ago.]
      I think, thererfor I am! But what I am, and why...?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Here's my solution; I'm sure there are others, probably better ones

      First get the children of the parent - I would use getIterator() so that it'll load one at a time into memory, instead of loading them all up at once.
      $count = 0;
      $docArray = $modx->getIterator('modResource',array(
          'parent' => 42,
      ));
      foreach($docArray as $doc) {
          $docid = $doc->get('id');
          $tv = $modx->getObject('modTemplateVar',array('name'=>'MyTV'));
          $rawValue = $tv->getValue($id); 
          if(isset($rawValue)) count++;
      }
      return $count;


      Thanks to Bob's Guides http://bobsguides.com/revolution-objects.html
        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
        • 29076
        • 615 Posts
        Thanks sottwell for helping me here. But unfortunately it didn't work. It crashed the page. I'm trying to figure out why. smiley

        I managed to get this code to work:
        $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvID, 'value' != $value));
        return $total;


        Snippet call:
        [[!tvValueCount? &tvId=`2` &value=``]]


        No I only have to figure out how to set the parent ID in the snippet code, so it doesn't search through all resources, and to count only published recources in that spesifik parent resource. [ed. note: Sylvaticus last edited this post 10 years, 6 months ago.]
          I think, thererfor I am! But what I am, and why...?
          • 4172
          • 5,888 Posts
          get the child_ids of the parent in an array first, then try:

          $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvID, 'value' != $value, 'contentid:IN' => $child_ids));
          return $total;

            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Ah, a stupid typo... that should be $count++

            With your snippet, getChildIDs() should do the job, you could use its output array in your getCount() query

            https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.getchildids
              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
              • 29076
              • 615 Posts
              Quote from: Bruno17 at Apr 05, 2016, 01:44 AM
              get the child_ids of the parent in an array first....

              Thanks Bruno17. But unfortunately php code and modx code is like reading Latin for my part, I'm just a html and css designer, my mind are just not able to understand php and modx code. I'm only hacking away on others good sugestions, like so many many others. So in this case I don't have the slitest clue on what you sugest to me. Sorry about that. smiley

              Can you please help me a little bit further? Meanwhile I will try to understand what you and Sottwell are trying to tell me.
                I think, thererfor I am! But what I am, and why...?
                • 3749
                • 24,544 Posts
                Bruno's solution is brilliant. To flesh it out (correct the parent ID in the tag):

                [[!tvValueCount? &tvId=`2` &parentId=`12` &value=``]]

                $parentId = $modx->getOption('parentId', $scriptProperties, '');
                $tvId = $modx->getOption('tvId', $scriptProperties, '');
                
                if (empty($parentId)) {
                    return 'No Parent Id Set';
                }
                
                if (empty($tvId)) {
                    return 'No TV Id Set';
                }
                
                $childIds = $modx->getChildIds($parentId);
                $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' != $value, 'contentid:IN' => $childIds));
                return $total;
                [ed. note: BobRay last edited this post 10 years, 6 months ago.]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 4172
                  • 5,888 Posts
                  this line has to be:

                  $childIds = $modx->getChildIds($parentId);
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 3749
                    • 24,544 Posts
                    Oops. Corrected above - thanks. smiley
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      I knew it was something like that wink
                        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