# Output User Profile Information from specific User Group

**URL:** <https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401>\
**Category:** General\
**Tags:** 2․6․x\
**Created:** [April 3, 2019, 9:13am UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401 "2019-04-03T09:13:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vibedesign](https://community.modx.com/user_avatar/community.modx.com/vibedesign/32/741_2.png) [@vibedesign](https://community.modx.com/u/vibedesign)\
**Post date:** [April 3, 2019, 9:13am UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/1 "2019-04-03T09:13:45Z")

</div>

### Background Information:

I’m working on a blog like page and I want an about page where the different authors are listed with their image and some other info. Every author has their own MODX user profile so this is where the information should be pulled from.

- MODX Revolution 2.6.5-pl
- PHP 7.2.16
- MySQL 5.7.25
- Extras: Ace, archivist, Articles, CKEditor, FormIt, getpage, getresources, MIGX, Quip, SimpleCart, taglister

### I want to achieve:

Probably an array (?) which grabs all user profiles, preferably from a specific user group so I can control which ones are shown and pull the wanted information from there. To make things more specific let’s say I want to grab their Name and their Photo.

### What I got so far:

Not too much honestly, I found [this post](https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done) in the old forums which seems to do sort of similar things and changed it as far as I could understand it to:

```
$profile = $modx->getObject('modUserProfile', array('internalKey' => $uid));
if ($profile) {
  $username= $profile->get('username');
  $photo= $profile->get('photo');
}

```

All of this is in a snippet `[[getUserInfo]]` but as it is right now it doesn’t do anything. I tried to `echo` the `$username` into my markup, but that didn’t show up. Is this the right start to tackle my issue? If so:

1. What am I doing wrong?
2. How do I define which user (group) gets picked?

Thanks alot for your help!

---

<div class="post-metadata">

**Author:** ![josh\_curtis](https://community.modx.com/user_avatar/community.modx.com/josh_curtis/32/47_2.png) [@josh\_curtis](https://community.modx.com/u/josh_curtis)\
**Post date:** [April 3, 2019, 12:54pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/2 "2019-04-03T12:54:18Z")

</div>

I just turned this up:

> <https://gist.github.com/pepebe/2483894>

I’d switch line 20 to `foreach($users as $user)` and then you should be able to use something very similar to it.

```auto
$username = $user->get('username');
$photo = $user->getOne('Profile')->get('photo');

```

Profiles are effectively split into two tables. Username is part of the ‘user’ table and ‘photo’ is part of the profile. It appears you’re querying the profile for the username above which would have lead to nothing being returned.

Here’s a breakdown of fields to help with what should come from which part of the user object (‘core’ vs ‘profile’): [https://docs.modx.com/revolution/2.x/administering-your-site/security/users](https://docs.modx.com/revolution/2.x/administering-your-site/security/users)

---

<div class="post-metadata">

**Author:** ![vibedesign](https://community.modx.com/user_avatar/community.modx.com/vibedesign/32/741_2.png) [@vibedesign](https://community.modx.com/u/vibedesign)\
**Post date:** [April 3, 2019, 2:13pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/3 "2019-04-03T14:13:43Z")

</div>

Thanks alot @jcdm, that (almost) worked great! 🙏

What’s not showing yet is the photo of the user. Here’s what my snippet looks like so far:

```
<?php
  $usergroup = 2;
  $c = $modx->newQuery('modUser');
  $c->innerJoin ('modUserProfile','Profile');
  $c->innerJoin ('modUserGroupMember','UserGroupMembers');
  $c->innerJoin ('modUserGroup','UserGroup','`UserGroupMembers`.`user_group` = `UserGroup`.`id`');
  // $c->leftJoin ('modUserGroupRole','UserGroupRole','`UserGroupMembers`.`role` = `UserGroupRole`.`id`'); // don't need this atm

  $c->where(array(
    'active' => true,
    'UserGroupMembers.user_group' => $usergroup,
    // 'UserGroupMembers.role' => '5', // don't need this atm
  ));

  $users = $modx->getCollection('modUser',$c);

  foreach($users as $user) {
    $username = $user->get('username');
    $photo = $user->getOne('Profile')->get('photo');
    $quote = $user->getOne('Profile')->get('city'); //renamed profile field city == quote
    $about = $user->getOne('Profile')->get('address'); //renamed profile field address == about
    
    echo '<div class="team red">
            <div class="profile">
                <img src="' . $photo . '" alt="' . $username . '">
            </div>
            <div class="quote">
                <h3>' . $username . '</h3>
                <p>"' . $quote . '"</p>
            </div>
            <div class="description">
                <p>' . $about . '</p>
            </div>  
        </div>';
  }

```

The two additional variables `$quote` and `$about` work fine. I just don’t get the image. The compiled code is empty: `<img src=""`

**Am I missing something?**

Update: I added a few more users to the role which is displayed, and oddly one of them has the image shown, so the problem might be somewhere else. Investigating…

**Also is this the appropriate way to get a PHP variables output in the HTML structure (by writing the HTML in the snippet)?**

---

<div class="post-metadata">

**Author:** ![josh\_curtis](https://community.modx.com/user_avatar/community.modx.com/josh_curtis/32/47_2.png) [@josh\_curtis](https://community.modx.com/u/josh_curtis)\
**Post date:** [April 3, 2019, 2:57pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/4 "2019-04-03T14:57:15Z")

</div>

Hmmm…one photo working but others not. That is intriguing. Do they all seem to have the same path in the field value? Are the names fairly long perchance?

I don’t see anything you’ve done wrong with the photo field. If the others are working then getting nothing from the photo field seems odd to me.

The best way to manage output is to move the HTML into a chunk. So, create a new chunk and put the HTML in there and give it a name.

Then, put your variables into an array instead and call getChunk with that array:

```auto
$userInfo = array();
$userInfo['username'] = $user->get('username');
...
echo $modx->getChunk('nameOfTheChunk',$userInfo);

```

Then in the chunk, username (and any other fields you’ve set) will be available in a placeholder. Placeholders look like `[[+this]]`

e.g. `<div class="team red"><h3>[[+username]]</h3></div>`

While there’s nothing wrong with what you’ve done, it makes it tricky to tweak presentation without messing with logic. The getChunk method is your friend for separating the two.

Let us know where you get to with the similarities/differences in photo paths.

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [April 3, 2019, 3:09pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/5 "2019-04-03T15:09:09Z")

</div>

You might look at the [Peoples snippet](https://docs.modx.com/extras/revo/peoples). I think it does most of what you want out of the box.

---

<div class="post-metadata">

**Author:** ![vibedesign](https://community.modx.com/user_avatar/community.modx.com/vibedesign/32/741_2.png) [@vibedesign](https://community.modx.com/u/vibedesign)\
**Post date:** [April 3, 2019, 3:12pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/6 "2019-04-03T15:12:06Z")

</div>

So turns out for some reason the image path wasn’t set anymore within the user profiles (except the one who worked obviously). Not sure why that happened, but will have a lookout for that in the future.

> [@jcdm](#):
>
> put your variables into an array instead and call getChunk with that array

Thanks a ton! You opened up a whole new world for me there, made my day! Always wondered how to combine chunks with snippets until now… 😊

> [@bobray](#):
>
> You might look at the [Peoples snippet](https://docs.modx.com/extras/revo/peoples). I think it does most of what you want out of the box.

@bobray Now that it’s working I’ll probably keep it as it is, but I will definitely look into that if I need something similar in the future! Thanks alot!

---

<div class="post-metadata">

**Author:** ![josh\_curtis](https://community.modx.com/user_avatar/community.modx.com/josh_curtis/32/47_2.png) [@josh\_curtis](https://community.modx.com/u/josh_curtis)\
**Post date:** [April 3, 2019, 3:18pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/7 "2019-04-03T15:18:51Z")

</div>

Here’s a helpful link on snippets that includes the chunk stuff I mentioned above.  
[https://docs.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet](https://docs.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet)

Turns out there’s at least one thing I’ve mislead you on now that I take a glance myself - specifically, do not print or echo.

Better to amass output via something like `$output .= $modx->getChunk(...)` then end with `return $output;` apparently. 👍🏻

---

<div class="post-metadata">

**Author:** ![vibedesign](https://community.modx.com/user_avatar/community.modx.com/vibedesign/32/741_2.png) [@vibedesign](https://community.modx.com/u/vibedesign)\
**Post date:** [April 3, 2019, 3:30pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/8 "2019-04-03T15:30:04Z")

</div>

> [@jcdm](#):
>
> Turns out there’s at least one thing I’ve mislead you on now that I take a glance myself - specifically, do not print or echo.

Thanks alot for guiding me to this anyway, I’ll look more into it!

I would like to mark your [first response](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/2) as the correct answer, but I can’t find a way to do that, am I not allowed to do this?

---

<div class="post-metadata">

**Author:** ![elizabeth](https://community.modx.com/user_avatar/community.modx.com/elizabeth/32/3_2.png) [@elizabeth](https://community.modx.com/u/elizabeth)\
**Post date:** [April 4, 2019, 1:59am UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/9 "2019-04-04T01:59:20Z")

</div>

Hey,

We had enabled this feature for the support sections. But your thread was started in the `General` section. I’ve enabled it in this section as a test, and we’ll monitor going forward from here to see if it is appropriate for this section.

---

<div class="post-metadata">

**Author:** ![vibedesign](https://community.modx.com/user_avatar/community.modx.com/vibedesign/32/741_2.png) [@vibedesign](https://community.modx.com/u/vibedesign)\
**Post date:** [April 4, 2019, 6:42am UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/10 "2019-04-04T06:42:31Z")

</div>

@elizabeth I see, so was my thread in the wrong section?  
If so feel free to move it of course! 😊

---

<div class="post-metadata">

**Author:** ![nuan88](https://community.modx.com/user_avatar/community.modx.com/nuan88/32/350_2.png) [@nuan88](https://community.modx.com/u/nuan88)\
**Post date:** [April 4, 2019, 2:35pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/11 "2019-04-04T14:35:23Z")

</div>

This thread is incredibly useful for dealing with users of a site.

There seem two key issues that are explained well: Getting the user tables (two tables not one, because of the extended profile), which is obviously important for many purposes.

The second thing is the use of chunks of html in snippets, for the output. This is something Modx does really well, if you know the code.

---

<div class="post-metadata">

**Author:** ![elizabeth](https://community.modx.com/user_avatar/community.modx.com/elizabeth/32/3_2.png) [@elizabeth](https://community.modx.com/u/elizabeth)\
**Post date:** [April 5, 2019, 6:20pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/12 "2019-04-05T18:20:44Z")

</div>

@vibedesign I wouldn’t say wrong section, we just hadn’t considered every avenue is all.

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [April 7, 2019, 6:20pm UTC](https://community.modx.com/t/output-user-profile-information-from-specific-user-group/401/13 "2019-04-07T18:20:49Z")

</div>

Closed on April 7, 2019, 6:20pm UTC

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.
