We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42415
    • 115 Posts
    Hi, Is it possible to dynamically display the total number of members to the front end of a modx website?

    I just want to use a sentence like:

    "There are now 272 members of website.com"

    Whereas every time a new member has been approved this number automatically changes to reflect the new number of new members..

    If using GetResources what would the code snippet be I have to use?

    Or, is there an alternative method that could be used?

    I'm currently using Modx 2.2.16 Traditional

    Thanks in anticipation

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

      • 36818
      • 119 Posts
      You can use the property
      &totalVar
      in getResources. The placeholder per default is
      [[+total]]


      E.g.
      [[getResources? &totalVar=`myTotal`]]
      Output: <strong>[[+myTotal]]</strong>
      
        • 42415
        • 115 Posts
        Quote from: achterbahn at Feb 13, 2016, 12:27 PM
        You can use the property
        &totalVar
        in getResources. The placeholder per default is
        [[+total]]


        E.g.
        [[getResources? &totalVar=`myTotal`]]
        Output: <strong>[[+myTotal]]</strong>
        

        Hi achterbahn,

        Thanks for taking the time to reply smiley

        I tried c&p your example onto a page but it gave the result Output:0

        I'm kind of assuming i've missed something really obvious here (i'm more at home with html and css than coding), but, how do I get it to correlate with the actual number of members of the website?

        fyi......i'm using Shaun McCormicks 'LogIn' extra for registration and have 200+ members.
          • 36818
          • 119 Posts
          You cannot achieve this with getResources. You'd rather have to write a snippet for this.

          /**
           * TotalUsers
           *
           * DESCRIPTION
           *
           * This Snippet counts the users
           *
           * PROPERTIES:
           *
           * &useractive integer optional
           *
           * USAGE:
           *
           * [[!TotalUsers? &useractive=`1`]]
           *
           */
          $useractive = $modx->getOption('useractive', $scriptProperties,1);
          
          
          // For debugging:
          $modx->log(modX::LOG_LEVEL_DEBUG
              , '[TotalUsers] called on page '. $modx->resource->id . ' with the following properties: '
              .print_r($scriptProperties, true));
          // Verify Inputs
          if (!isset($scriptProperties['useractive'])) {
              $modx->log(modX::LOG_LEVEL_ERROR, '[TotalUsers] missing required properties &useractive!');
              return;
          }
          $output = '';
          
          $users = $modx->getCollection('modUser',array('active'=>$useractive));
          
          if (empty($users)) {
          
          $output = "No Users found.";
          
          }  else {
          
          $output = count($users);
          }
          
          return $output;


          Is this more you are looking for?
          • discuss.answer
            • 3749
            • 24,544 Posts
            This might be a little faster and more efficient, since getCollection() will actually retrieve every user's data even though you don't need it.

            If you want a count of all users active or not, you can do it with a single line, since there will always be at least one user (the admin):

            <p>There are [[!totalUsers]] total users on this site</p>

            /* totalUsers snippet */
            return $modx->getCount('modUser');


            If you only want active users it would be:
            return $modx->getCount('modUser', array('active' => '1'));


            This has nothing to do with getResources and I would put it in the page Template.

            Unless it's critical that it be up-to-date, I would call it cached since that would give you faster page loads and it would be updated every time the cache is cleared (which happens whenever a page or element is saved).

            [[totalUsers]]

              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
              • 42415
              • 115 Posts
              Quote from: BobRay at Feb 13, 2016, 08:46 PM
              This might be a little faster and more efficient, since getCollection() will actually retrieve every user's data even though you don't need it.

              If you want a count of all users active or not, you can do it with a single line, since there will always be at least one user (the admin):

              <p>There are [[!totalUsers]] total users on this site</p>

              /* totalUsers snippet */
              return $modx->getCount('modUser');


              If you only want active users it would be:
              return $modx->getCount('modUser', array('active' => '1'));


              This has nothing to do with getResources and I would put it in the page Template.

              Unless it's critical that it be up-to-date, I would call it cached since that would give you faster page loads and it would be updated every time the cache is cleared (which happens whenever a page or element is saved).

              [[totalUsers]]


              Hi BobRay,

              Yes that works perfectly smiley

              Thanks for taking the time to show me how to do this.
                • 46886
                • 1,154 Posts
                I did not know how much I need this until now. Thanks to all!