Show User Info Based on a TV I

Select from a drop-down list of users and show the selected user's information on a page

MODX Forum user Andy Jones wanted a way to select a user from a list of all users in a TV and then show that user's information on the page. This is actually pretty easy. We'll see how to do it in this article.

MODX logo

The Problem

Suppose you have a site showing galleries for a variety of artists. You want to contact information about the artist on the artist's gallery page. All your artists are registered users on the site so they can upload new images. You could write a custom snippet to show the data and enter the user's username or ID in the snippet tag, but you'd have to look up each user to confirm the username or ID. It would be much nicer if you could select the user from a drop-down list when editing their page.

The Solution

The first step is to create the TV. Let's call it GalleryUser. On the "Input Options" tab, set the TV Type to Listbox (single-select). Then, put this in the "Input Option Values" field:

@SELECT `username`,`id` FROM `modx_users` WHERE `active` = 1

You can leave the other fields as they are. Note that you can only have two fields in the MySQL statement. The values from the first field will be displayed in the drop-down list, the values from the second field will be stored as the TV values.

Displaying the Results

Next, we'll use the Profile snippet to show the user's information. The Profile snippet is part of the Login package, so make sure the Login extra is installed. Put this code the page template (or in the Resource Content field):

[[!Profile? &user=`[[+GalleryUser]]`]]

<p>Username: [[+username]]</p>
<p>Full Name: [[+fullname]]</p>
<p>Email: [[+email]]</p>
<p>Phone: [[+phone]]</p>

You can use any placeholders from the user or user profile object. If you need to use extended fields, do this:

[[!Profile? &user=`[[*GalleryUser]]` &useExtended=`1`]]

Be sure that the Profile snippet tag is above all the placeholders and remember that even though you selected the username from the drop-down list, it is the selected user's ID that is stored as the TV value for that page and retrieved by the placeholder.

Coming Up

This version is nice, but it shows every user on the site. In the next article we'll see how to show just the users in a particular user group.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.