We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I want to do something like this...

    $user = $modx->getUser();
    foreach ( $user as $key => $value ) {
        $placeholders[$key] .= $value;
    }
    


    But I know that won't work out the way I want it to.

    What is the correct way to iterate through the user table fields?

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

      • 3749
      • 24,544 Posts
      I'm not not clear what you're trying to do, but this might be what you want:

      Create a Tpl chunk called UserChunk with the fields you want to show and some formatting:

      <p>
         Username: [[+username]]<br/>
         email: [[+email]]<br/>
         Full Name: [[+fullname]]<br/>
         etc.
      </p>
      


      Put this code in your snippet
      $users = $modx->getCollectionGraph('modUser', '{"Profile":{}}');
      
      $output = "<h3>User List</h3>";
      
      foreach($users as $user) {
          $fields = $user->toArray();
          unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
          if ($user->Profile) {
              $fields = array_merge($user->Profile->toArray(), $fields);
          }
       
          $output .= $modx->getChunk('UserChunk', $fields);
      }
      
      return $output;
      
        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
        • 49407
        • 159 Posts
        Quote from: BobRay at Aug 13, 2015, 03:41 AM
        I'm not not clear what you're trying to do, but this might be what you want:

        Create a Tpl chunk called UserChunk with the fields you want to show and some formatting:

        <p>
           Username: [[+username]]
        
           email: [[+email]]
        
           Full Name: [[+fullname]]
        
           etc.
        </p>
        


        Put this code in your snippet
        $users = $modx->getCollectionGraph('modUser', '{"Profile":{}}');
        
        $output = "<h3>User List</h3>";
        
        foreach($users as $user) {
            $fields = $user->toArray();
            unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
            if ($user->Profile) {
                $fields = array_merge($user->Profile->toArray(), $fields);
            }
         
            $output .= $modx->getChunk('UserChunk', $fields);
        }
        
        return $output;
        

        Thanks for your reply, BobRay.

        That's a great example of iterating through multiple users, however, I would like to iterate through every field of a single user. I am trying to build a loop that will assign each of a single user's fields into a $placeholders array. So far it seems like I have to use the $user->get('field') to get each of the users fields from the database. I really wish I could iterate through a single users database fields in a loop rather than write out numerous get() calls.
          • 3749
          • 24,544 Posts
          Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

          [[!Profile]]
            
          <p>Username: [[+username]]</p>
          <p>Full Name: [[+fullname]]</p>
          <p>Email: [[+email]]</p>
          



          If you prefer to do it in code, it's just like my example above without the loop, and with getObjectGraph() instead of getCollectionGraph().

          $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
          fields = $user->toArray();
          unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
          if ($user->Profile) {
             $fields = array_merge($user->Profile->toArray(), $fields);
          }
            
          $output .= $modx->getChunk('UserChunk', $fields);
          
          
            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
            • 49407
            • 159 Posts
            Quote from: BobRay at Aug 13, 2015, 06:59 PM
            Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

            [[!Profile]]
              
            <p>Username: [[+username]]</p>
            <p>Full Name: [[+fullname]]</p>
            <p>Email: [[+email]]</p>
            



            If you prefer to do it in code, it's just like my example above without the loop, and with getObjectGraph() instead of getCollectionGraph().

            $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
            $fields = $user->toArray();
            unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
            if ($user->Profile) {
               $fields = array_merge($user->Profile->toArray(), $fields);
            }
              
            $output .= $modx->getChunk('UserChunk', $fields);
            
            

            OK, I see how that works. My placeholders are going to need to be [[!+fi.fieldname]] to work with formit. How do I amend the placeholders that are output? [ed. note: BobRay last edited this post 8 years, 8 months ago.]
              • 3749
              • 24,544 Posts
              [[!Profile? &prefix=`fi.`]]
                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
                • 49407
                • 159 Posts
                Quote from: BobRay at Aug 13, 2015, 06:59 PM
                Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

                $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId);
                
                

                Is there a way to set a prefix on the placeholders using this method?

                I prefer to do it with code because I have my own pre-processor gathering other custom data for the user settings such as language preference and display name/alias. I also have a myriad of settings in another table for privacy settings, user blocking, and security.
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  You might look at ClassExtender for storing the extra user information.

                  To set placeholders with a prefix:

                  $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
                  $fields = $user->toArray();
                  unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
                  if ($user->Profile) {
                     $fields = array_merge($user->Profile->toArray(), $fields);
                  }
                   
                  $modx->toPlaceholders($fields, 'fi');
                    
                  return $modx->getChunk('UserChunk');


                  You can do whatever you want with the $fields array before calling toPlaceholders().
                    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
                    • 49407
                    • 159 Posts
                    Quote from: BobRay at Aug 13, 2015, 09:22 PM
                    You might look at ClassExtender for storing the extra user information.

                    To set placeholders with a prefix:

                    $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
                    $fields = $user->toArray();
                    unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
                    if ($user->Profile) {
                       $fields = array_merge($user->Profile->toArray(), $fields);
                    }
                     
                    $modx->toPlaceholders($fields, 'fi');
                      
                    return $modx->getChunk('UserChunk');


                    You can do whatever you want with the $fields array before calling toPlaceholders().

                    Thanks again, BobRay! I've been searching for a couple days trying to figure out the trick to doing this. Now I know. I'll take a look at that article also.