We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29806
    • 8 Posts
    Anyone know of a snippet to list all users if not would anyone be willing to help me find one? or point me in the right direction in coding one? Thank you.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You’ll need to get it from the database. What information do you need? Just usernames?
        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
        • 29806
        • 8 Posts
        I am a newb coder but I did this quick non modx code.
        <?php
        # USERLIST UNSECURE
        # Created By Ryan
        #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        
            mysql_connect("localhost", "root") or
                die("Could not connect: " . mysql_error());
            mysql_select_db("modx");
        
            $result = mysql_query("SELECT * FROM modx_web_users");
        
            while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                printf ("<b>%s</b> %s <BR />", $row[0], $row[1]);  
            }
        
            mysql_free_result($result);
        
        ?>


        I included this file with a include(); I know this is unsecureish anyone help me know the modx way?

        this outputs:
        1 User1
        2 User2 etc

        I know this is unsecure
          • 22303 MODX Staff
          • 10,725 Posts
          Here is an example the MODx way...in a snippet, you’ll want to include the following code... (please note no PHP tags required)

          $output= '<ul>' . "\n";
          if ($ds= $modx->db->select("*", $modx->getFullTableName('web_users'), "", "username", "")) {
          	while ($row= $modx->db->getRow($ds)) {
          		$userId= $row['id'];
          		$userName= $row['username'];
          		$output.= "<li><strong>$userId</strong> $userName</li>\n";
          	}
          }
          $output.= '</ul>' . "\n";
          return $output;


          This is using the internal DBAPI and the getFullTableName() function so it will work on any installation, and shares the internal DB connection. Oh and it also sorts by username. Let me know if you have any questions about any of this.
            • 29806
            • 8 Posts
            Thanks man that should get me there! My main trouble was getting to the database the appropriate way.
              • 6726
              • 7,075 Posts
              Should this be listed as a UserList snippet in the appropriate forum ?
                .: COO - Commerce Guys - Community Driven Innovation :.


                MODx est l&#39;outil id
                • 25663 MODX Staff
                • 12,272 Posts
                Sure, why not...
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 29806
                  • 8 Posts
                  One thing I am going to try to expand this code first with pagination then I want to try to make a user profiles section.
                    • 22303 MODX Staff
                    • 10,725 Posts
                    This will be ok for now as a temporary way to do this, but the upcoming API changes surrounding users, groups, roles, authentication, permissions, and user profiles will have methods for doing this without getting your hands dirty with SQL.
                      • 29806
                      • 8 Posts
                      Any clue when the new API can be expected?