We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I’m trying to be very clever and make the fullest possible use of the MODx DBAPI. I have two queries to generate a list of all users in a given web group:
    $userlist = '';
    $table1 = $modx->getFullTableName('web_users');
    $table2 = $modx->getFullTableName('webgroup_names');
    $table3 = $modx->getFullTableName('web_groups');
    
    $result = $modx->db->select("id",$table2,"name = '".$webgroup."'");
    $groupid = $modx->db->getValue($result);
    
    $result = $modx->db->select("tbl1.username","$table1 AS tbl1, $table2 AS tbl2, $table3 AS tbl3","tbl3.webgroup=".$groupid." AND tbl1.id = tbl3.webuser", "tbl1.username ASC");
    
    while($users = $modx->db->getRow($result)) {
      $userlist .= $users['username'].'<br />';
    }
    return $userlist;
    

    I know this could be done with a single query. Can anybody with better SQL skills show me how it should be done?
      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
      • 15987
      • 786 Posts
      try this:

      $result = $modx->db->select("tbl1.username","$table1 AS tbl1, $table2 AS tbl2, $table3 AS tbl3","tbl2.id = tbl3.webgroup AND tbl2.name = '".$webgroup."' AND tbl1.id = tbl3.webuser", "tbl1.username ASC");
      
        • 19726
        • 239 Posts
        I had the same question some time ago. This was the answer of OpenGeek that worked for me: http://modxcms.com/forums/index.php/topic,6764.msg47463.html#msg47463
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          That did the trick! Thank you!

          Jason’s way works also, but I was, as I said, trying to be clever and do it all in one MODx-ish statement!
            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