• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Grabbing user's avatar from just a string

#1
Hi guys! I was wondering if it was possible to use <xen:avatar> with just a string.

I have this snip of code:
Code:
<xen:avatar user="$player" size="s" img="true" />
                    <xen:username user="$player" rich="true" />
                    <div class="userTitle">{xen:helper userTitle, $player>
Essentially, I am grabbing a list of players on a server that will have the exact same name on the forums, and I'd like to have the list of in-game players link to their profiles. Any suggestions?


Side note: is it possible to use the bounding avatar boxes with different images? Thanks!!
 

Jake Bunce

XenForo moderator
Staff member
#2
I'm not sure what you mean.

xen:avatar is a function that can be called inside of XF's templates. You pass it a user record and it displays their avatar.

What "string" are you working with?
 
#3
Sorry, I thought I was clear enough. ;)

So I have an array of strings containing a usernames, and I wanted to show their avatar withouth having an actuall user object, unless of course I can create a user object from just the username (I don't have their UIDs).
 
#6
Okay, so. Here is an update on what I have:
  • I have user object arrays created in a separate PHP script.
  • The usersnames / user_ids seem to work correctly, the links and user cards work.
  • Avatars refuse to show up, they are always the default avatars even though the users name/id is correct.
Here is the script I am using to make the users:

Script in add-on listener:
PHP:
$mcusers = array();
           
            foreach($players as $play)
            {
                $temparray = array();
                $temparray["username"] = $play;
                $temparray["user_id"] =  MinecraftUsers_Minequery::userid($play);
                $mcusers[] = $temparray;
            }
MinecraftUsers_Minequery::userid:
PHP:
$username = $_GET["mcname"];
        // Make a MySQL Connection
        mysql_connect("***********", "********", "******") or die(mysql_error());
        mysql_select_db("*******_communty") or die(mysql_error());
 
        // Retrieve all the data from the "example" table
        $result = mysql_query("SELECT * FROM `xf_user` WHERE username='" . $username . "'")
        or die(mysql_error()); 
 
        // store the record of the "example" table into $row
        $userinfo = mysql_fetch_array( $result );
        // Print out the contents of the entry
 
        $user = array();
        $user['user_id'] = $userinfo["user_id"];
        $user['username'] = $userinfo["username"];
       
        return $user;
And finally displaying the users on the side bar:
Code:
<xen:if hascontent="true">
    <div class="section">
        <div class="secondaryContent">
            <h3>Minecraft Users Online</h3>
            <ol class="listInline">
            <xen:contentcheck>
                <xen:foreach loop="$players" value="$player">
                                <li>
                    <xen:avatar user="$user" size="s" img="true" />
                </li>
                </xen:foreach>
                           
            </xen:contentcheck>
            </ol>
            <div class="footnote">
                Online now: {xen:count $players}
            </div>
        </div>
    </div>
</xen:if>
Any help would be awesome guys :)
 

Fuhrmann

Well-known member
#7
Instead of passing just the user_id and username, pass all the user info! Try and see if it works.

Try this:

PHP:
        $username = $_GET["mcname"];
        // Make a MySQL Connection
        mysql_connect("***********", "********", "******") or die(mysql_error());
        mysql_select_db("*******_communty") or die(mysql_error());
 
        // Retrieve all the data from the "example" table
        $result = mysql_query("SELECT * FROM `xf_user` WHERE username='" . $username . "'")
        or die(mysql_error());
 
        // store the record of the "example" table into $row
        $userinfo = mysql_fetch_array( $result );
        // Print out the contents of the entry
      // Just return all the data
 
        return  $userinfo;

You may change the listener too:

PHP:
$mcusers = array();
         
            foreach($players as $play)
            {
                $temparray = array();
                $temparray =  MinecraftUsers_Minequery::userid($play);
                $mcusers[] = $temparray;
            }
 

Jake Bunce

XenForo moderator
Staff member
#10
How is this interfacing with the forum? Are you using a template hook or some other callback to add on to the sidebar?

Make sure the user record is made available to the template otherwise it won't work.
 
#11
How is this interfacing with the forum? Are you using a template hook or some other callback to add on to the sidebar?

Make sure the user record is made available to the template otherwise it won't work.
It is being passed to a template hook on the side bar. Sorry, I am really new to programming in this context, how may I ensure the availability of the user record in the template?