We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6506
    • 35 Posts
    I have used the getChildCount snippet to pull back the number of items in a parent https://forums.modx.com/thread/43563/solved-getresources-and-counting-children This works well in counting items under one ID, but I am trying to modify it to count child elements from multiple resources (so child resources of ID 1,2,3,4 etc).

    Basically I have this structure

    Some ID > Multiple Child Elements > multiple items in these child elements

    I am trying to get the count of items in each multiple child element so ([[getChildCount? &parent=`57,58,59,60` ]]) but that will only bring back the count in the first one, how do I get each count and have them added into one total. Any help appreciated. Thanks
      • 4172
      • 5,888 Posts
      you can use the api-function getChildIds to get all children of a given resource with a given tree-depth and count the result.
      http://rtfm.modx.com/display/revolution20/modX.getChildIds
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 6506
        • 35 Posts
        Hi Bruno, thanks for the reply but I am not sure how I would implement this. Can I modify the getChildCount snippet to do this?

        <?php
        $count = 0;
        $parent = isset($parent) ? (integer) $parent : 0;
        if ($parent > 0) {
            $criteria = array(
                'parent' => $parent,
                'deleted' => false,
                'published' => true,
            );
            $count = $modx->getCount('modResource', $criteria);
        }
        return (string) $count;
        
        


        Where would I do that? Or would I create a new snippet? I am not a programmer unfortunately so any help appreciated. Thanks
          • 6506
          • 35 Posts
          OK, I have a better solution that will work for me and will make my categorization easier. This is using the getChildIds and is querying the database to pull back the value associated with it.

          <?php
          $total = '0';
          $parentId = !empty($parentId) ? $parentId : 0;
          $childIds = $modx->getChildIds($parentId, 1);
          
          if (!empty($value) && !empty($childIds)) {
              $query = "SELECT COUNT(`value`) FROM " . $modx->getFullTableName('adf_site_tmplvar_contentvalues') . " WHERE `tmplvarid` = {$tmplVarId} AND `value` = '{$value}' AND `contentid` IN (" . implode(',', $childIds) . ")";
              if ($result = $modx->db->query($query)) {
                  $total = $modx->db->getValue($result);
              }
          }
          
          return (string) $total;


          To explain further I have a list of jobs in different sectors and each job will be in a given country. Based on that I can use the getChildCount snippet to pull back the number of jobs in each sector. Then use the GetTVCount snippet to run through each job and find the TV associated with the country so for example the UK and count that also, thereby displaying total jobs in each sector or country.

          The problem I have got is that the query comes back with the error

          Call to undefined method modX::getFullTableName()

          I assume the issue is that this query was originally for modx evo but I can't find how the query should be put together for revo. Anyone any ideas. Thanks in advance.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            If you use the xPDO API you don't need all that.


            http://rtfm.modx.com/display/revolution20/Accessing+Template+Variable+Values+via+the+API
            http://rtfm.modx.com/display/xPDO20/xPDO.getCount [ed. note: sottwell last edited this post 13 years, 8 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
              • 6506
              • 35 Posts
              Hi Susan, thanks for that I see it should be easier but have tried and get no results or errors. I have a snippet

              $page = $modx->getObject('modResource', 41);
              return $page->getTVValue('Dubai');
              
              $query = $modx->newQuery('Dubai',array());
              $total_country = $modx->getCount('Dubai',$query);
              $query->limit(10, 0); 
              
              
              $modx->log(modX::LOG_LEVEL_ERROR, "Dubai: $total_country");
              


              which I am trying to use to just get the value of a child element in one ID which has a TV value of Dubai. This doesn't work although what I really need to do is get the number of resources with the TV value of Dubai etc from 20 different parent ID's. I'm obviously not calling it correctly or accessing the api correctly as it is not doing anything but I can't find any examples to go off.

              Is there anywhere I can get a bit more in-depth info. Thanks
                • 6506
                • 35 Posts
                OK, I can't seem to wrap my head around Revo and xpdo. I'm trying to right a snippet which will pull back and count only the number of items with a TV of X but just seem to be going nowhere. I have looked at the getResources snippet to see examples and I assume I have to start with $modx->newQuery() and somewhere use the $criteria [] = and try and pull back the Value from the database of the ID of the Template Variable. I just seem to get errors or no output what ever I do.

                Any help really would be much appreciated.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  It should be something like this
                  $total = $xpdo->getCount('modTemplateVarResource',array(
                     'value' => 'whatever the value should be'
                  ));

                  but that's not doing anything. I know that's close, though.
                    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
                    • 3749
                    • 24,544 Posts

                    I'm not clear on what you want to do. If you just want the count of resources that have that TV:

                    $tvId = 12;
                    $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId));


                    If you want the number of those TVs that hold a particular value, it would be:

                    $tvId = 12;
                    $value = 'somevalue'
                    $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $value));


                    If you need to get the Resources (or Resource ID's) for the Resources that have the particular value for the TV, you'd have to use getCollection():

                    $tvID = 12
                    $value = 'somevalue'
                    $tvrs = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $value));
                    
                    foreach ($tvrs as $tvr) {
                        $resourceId = $tvr->get('contentid');
                        $doc = $modx->getObject('modResource', $resourceId);
                        if ($doc) {
                            $fields = $doc->toArray();
                            echo 'Pagetitle: ' . $fields['pagetitle'];
                            // etc.
                        }
                    
                    }


                    You could probably do the last one in a single query with a join or with getCollectionGraph() but I'm too tired to figure out the code. wink




                      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 figured out that I needed to add that "tmplvarid" parameter at about 3 AM 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