We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36467
    • 73 Posts
    Is there any way, i mean API through we can get all users list ?? I am creating app in which i am going to list all the users and their profile, subscriptions etc. Below is what i am so far.

    $profile = $modx->user->getOne('Profile');
    $fields = $profile->get('fullname');

    how can i loop this code for all users ?? [ed. note: amitpatil last edited this post 11 years, 8 months ago.]
      • 1778
      • 659 Posts
      Hi
      Not tested but probably something like

      $c = $modx->newQuery('modUser');
      $users = $c->getCollection($c);
      
      foreach($users as $user){
          $profile = $modx->user->getOne('Profile');
          $fields = $profile->get('fullname');   
          // do the stuff you want for each user as format output or other stuff
          ...
      }
      
        • 36467
        • 73 Posts
        Thanks Anso for this quick help, Your code worked with little modification
        $c = $modx->newQuery('modUser');
        $users = $modx->getCollection("modUser",$c);
        
        foreach($users as $user){
            $profile = $modx->user->getOne('Profile');
            $fields[] = $profile->get('fullname');  
            // do the stuff you want for each user as format output or other stuff
        
        }
        
        print_r($fields);
        


        There is little problem i am getting same username 200 times, I hope u understand whats happening. Its looping for all 200 users but showing the same info.

        I am not lazy, but i wasnt able to find any detailed documentation for this.
          • 36467
          • 73 Posts
          I got it solved, and here my final code. I am posting it in case it help someone like me, hopefully tongue
          <?php
          error_reporting(E_ALL);
          require_once 'config.core.php';
          require_once MODX_CORE_PATH.'model/modx/modx.class.php';
          $modx = new modX();
          $modx->initialize('web');
          
          $c = $modx->newQuery('modUser');
          $c->leftJoin('modUserProfile','Profile');
          
          // add column names that u want to show
          $c->select(array(
              'modUser.*',
              'Profile.fullname',
              'Profile.email',
              'Profile.blocked',
          ));
          
          $users = $modx->getCollection("modUser",$c);
          
          foreach($users as $user){
              $userArray = $user->toArray();
              echo "
          ".$userArray['username']."==".$userArray['fullname'];
          }
          
          ?>
          
            • 3749
            • 24,544 Posts
            There is also the Peoples snippet, which is a convenient way to do the same thing, though your code might be faster.


            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODX info for everyone: http://bobsguides.com/modx.html
              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
              • 36467
              • 73 Posts
              thanks BOB, I will try this, for now my code is working fine. I am developing big app so i will need ur help in future also.
              • This proved to be very useful to me as well, since I need to list users with a certain WHERE clause. This proved to be easier than using Peoples or pdoUsers. I just added the WHERE clause:
                $c->where(array(
                   'Profile.zip:=' => $zip,
                   'AND:Profile.comment:=' => $comment,
                   'AND:Profile.fax:=' => $fax
                ));
                  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
                • I also added a bit at the end to properly template the output.
                  foreach($users as $user){
                      $userArray = $user->toArray();
                      $output .= $modx->getChunk('userListTpl', $userArray);
                  }
                  return $output;
                    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