We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29076
    • 615 Posts
    Thanks! laugh It works just great. Now I can make all my statistical pages. I’ll post the result here as soon as I have made the styling.
      I think, thererfor I am! But what I am, and why...?
      • 17412
      • 270 Posts
      I’ve been looking at a way to incorporate a "count only published documents" element to the snippet. On a simplistic level I added

      $published='1'


      To the beginning though I realise that I’ll likely need to expand the SQL query to select from modx_site_content where published =1

      Can anyone help?
        • 17412
        • 270 Posts
        Sorry to bump.

        I’ve being looking through the Modx methods trying to figure a way of only pulling active Child ID’s instead of the all child id’s as getChildIds does. I don’t think there is an inbuilt method for this so I’m assuming this will need to be part of the sql query and I’m fairly sure it’ll involve something like this:

        $modx->getFullTableName('modx_site_content'), 'published > 0'


        Someone put me out of my misery please :’(
          • 10487 MODX Staff
          • 1,535 Posts
          @Lokust: Something like this maybe:
          <?php
          $total = '0';
          $parentId = !empty($parentId) ? $parentId : 0;
          $childIds = $modx->getChildIds($parentId, 10); /* go 10 levels down in document tree */
          if (!empty($childIds)) {
              $query = "SELECT COUNT(id) FROM ".$modx->getFullTableName('site_content')." WHERE `published`=1 AND `id` IN (" . implode(',', $childIds) . ")";
              if ($result = $modx->db->query($query)) {
                  $total = $modx->db->getValue($result);
              }
          }
          return (string) $total;
          ?>
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 17412
            • 270 Posts
            Thanks Garyyn. Is there a way to combine that sql query with Jason’s query?

            eg:

            $query = "SELECT COUNT(`value`) FROM " . $modx->getFullTableName('site_tmplvar_contentvalues') . " WHERE `tmplvarid` = {$tmplVarId} AND `value` = '{$value}' AND `contentid` IN (" . implode(',', $childIds) . ") AND $modx->getFullTableName('site_content')." WHERE `published`=1";


            Jason’s snippet does perfectly what I need to do apart from it grabs all items (published and unpublished and deleted for that matter)
              • 10487 MODX Staff
              • 1,535 Posts
              Sorry lokust! I completely neglected to read the opening posts of this topic and missed the whole of the TV discussion - anyhow, try this (untested at my end but should work):
              <?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('site_tmplvar_contentvalues') . " WHERE `tmplvarid` = {$tmplVarId} AND `value` = '{$value}' AND `contentid` IN ((SELECT id FROM ".$modx->getFullTableName('site_content')." WHERE id IN(" . implode(',', $childIds) . ") AND published=1 AND deleted=0))";
                  if ($result = $modx->db->query($query)) {
                      $total = $modx->db->getValue($result);
                  }
              }
              return (string) $total;
              ?>

              Basically, it’s using a sub-query to retrieve the document IDs for the main query. Hope it helps!
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 17412
                • 270 Posts
                Thanks Garryn.

                I get this error using that code:

                « MODx Parse Error »
                MODx encountered the following error while attempting to parse the requested resource:
                « Execution of a query to the database failed - Subquery returns more than 1 row »
                      SQL: SELECT COUNT(`value`) FROM `dbname`.`modx_site_tmplvar_contentvalues` WHERE `tmplvarid` = 25 AND `value` = 'Dining' AND `contentid` IN ((SELECT id FROM `dbname`.`modx_site_content` WHERE id IN(16,111,99,230,218,219,96,80,231,233,235,236,60,59,58,220,221,225,226,227,109,113,114,103,228,102,98,97,229,224,223,222,217,57,56,74,73,72,71,70,69,68,67,95,66,79,76,75,65,55,54,53,214,208,22,61,62,63,64,78,14,237,94,239,238,52,50,49,234,48,24,47,232,51) AND published=1 AND deleted=0))
                  • 22303 MODX Staff
                  • 10,725 Posts
                  I’d use a JOIN for this to get a count:
                  <?php
                  $total = '0';
                  $parentId = !empty($parentId) ? $parentId : 0;
                  $childIds = $modx->getChildIds($parentId, 1);
                  if (!empty($value) && !empty($childIds)) {
                      $query = "SELECT COUNT(`cv`.`value`) FROM " . $modx->getFullTableName('site_tmplvar_contentvalues') . " `cv` JOIN ".$modx->getFullTableName('site_content')." `sc` ON `cv`.`contentid` IN (" . implode(',', $childIds) . ") AND `cv`.`tmplvarid` = {$tmplVarId} AND `cv`.`value` = '{$value}' AND `sc`.`id` = `cv`.`contentid` AND `sc`.`published` = 1 AND `sc`.`deleted` = 0";
                      if ($result = $modx->db->query($query)) {
                          $total = $modx->db->getValue($result);
                      }
                  }
                  return (string) $total;
                  ?>
                    • 17412
                    • 270 Posts
                    Spot on smiley
                      • 813 ☆ A M B ☆
                      • 326 Posts
                      This works great! One small issue, and I can’t figure out how to fix it. For some of my TVs, I’m allowing multiple values, and then using the Delimited List widget (using comma as the delimiter, although it still has the double pipes in the DB’s "value" field). Would it be possible to modify this to somehow explode and iterate through each value? Or is there a way to do it directly in the DB query?