1. 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

Discussion in 'XenForo Development Discussions' started by Jake Hakoda Shirley, Feb 7, 2012.

  1. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    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!!
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    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).
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    You will need at least the user_id. You can't do it with just a username.
     
  5. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    So is there a way to grab the user_id using the username?
     
  6. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    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 :)
     
  7. Fuhrmann

    Fuhrmann Well-Known Member

    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;
                }
     
    Bob likes this.
  8. Bob

    Bob Well-Known Member

    you need avatar_date and you should also grab avatar_width, avatar_height and gravatar
     
  9. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    Thanks for all the help guys! :)

    I am running into a problem though. I made the above changes and now the sidebar displays blank avatars without links. Any ideas?
     
  10. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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. Jake Hakoda Shirley

    Jake Hakoda Shirley Active Member

    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?
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  13. gordy

    gordy Well-Known Member

    Did you get that to work? If so can you share it ?
     

Share This Page