View Full Version : Avatar display issue in template
DataHero
01-20-2011, 12:05 PM
Hello,
Considering I don't like to bump pretty old threads, thought I'd make a new one. I've been focusing on getting the avatar to display properly in showgroups.php; it should also update as staff members change the avatar.
So far I've gotten pretty far, although I'm a bit stuck now. From my understanding, I can use the fetch_avatar_url() function as to fetch the current avatar url. As such, I did the following:
# Require the user functions in includes
require_once('./includes/functions_user.php');
# Declare variable for global userid and username
$userid = $vbulletin->userinfo['userid'];
$uname = $vbulletin->userinfo['username'];
# Fetch the avatar URI
$avauri = fetch_avatar_url($userid);
# Using an if condition to check whether there is an uri available or not. If not, change uri to that of the 'No avatar' avatar.
if(!empty($avauri)) {
$dh_avauri = '<a href="member.php?'.$session[sessionurl].'u='.$userid.'"><img src="image.php?u='.$userid.'" width="120" height="120" border="0" alt="'.$uname.'\'s avatar"></a>';
} else {
$dh_avauri = '<a href="member.php?'.$session[sessionurl].'u='.$userid.'"><img src="$stylevar[imgdir_misc]/noavatar/noavatar.gif" width="120" height="120" border="0" alt="'.$uname.'\'s avatar"></a>';
}
And here in the template; used it only to test so the layout isn't as intended ATM (showgroups_usergroup):
<tr>
<td class="thead"> </td>
<td class="thead" width="100%">$vbphrase[user_name]
$dh_avauri
<if condition="$show['locationfield']"> / $vbphrase[location_perm]</if></td>
<if condition="$show['contactinfo']"><td class="thead">$vbphrase[contact]</td></if>
</tr>
It did display an avatar, but just that of mine, and the ID of the anchor tag remains at '1' as well. I also see all avatars as the same one; mine.
Help would be greatly appreciated!
It's always showing yours because you're using the userid and username from $vbulletin->userinfo, which is the logged in user.
ETA: if your code is in that main loop in showgroups.php, it looks like you'd want to use $user['userid'] and $user['username'] instead.
DataHero
01-20-2011, 12:36 PM
Thanks for the quick response. Much appreciated.
I've searched through the showgroups.php file and I could not seem to figure out which variable to use then. Considering $vbulletin->userinfo[] is for the logged in user, which should I use instead?
If I use $user[userid], $user['userid'], $userinfo['userid'], or $userinfo[userid], I get either a DB error as it would say 'WHERE user.id=', with no id at all. Or otherwise, I would simply have no ID at the end of the URL of the avatar/user.
Using $user[userid] and $user[username] should work, but only if your code is inside the loop that starts with:
ksort($users); // alphabetically sort users
$usergroupbits = '';
foreach ($users AS $user)
{
and it looks like the only hook in there is showgroups_user. Where are you putting the code you posted?
DataHero
01-20-2011, 12:55 PM
Alright, I've changed the hook to showgroups_user and also have added the if condition into the foreach loop. The avatars changed according to the user now, along with the username and ID. Thanks. :)
However, if I change it in my UserCP, showgroups.php shows the old avatar still. Why is this happening, considering as far as I know, the forum uses the filesystem and/or DB?
EDIT: Also, the usernames seem to have vanished; only avatar is visible now. Tried reverting template, but that didn't work.
However, if I change it in my UserCP, showgroups.php shows the old avatar still. Why is this happening, considering as far as I know, the forum uses the filesystem and/or DB?
I happen to know this one (I think) because someone asked about it not too long ago - the browser caches the image, and unless you put something on the image URL to make it unique, the browser won't see a change. You're calling fetch_avatar_url() which takes care of that (I didn't know that existed), but you're not using the returned value. I think you want something like:
<img src="$aviurl" ... (wrong - see below)
I don't know about the user names.
DataHero
01-20-2011, 01:37 PM
Well this is getting into the right direction! :)
I've replaced the img tag's src with:
<img src="'.(implode($avauri)).'" />
I've tried adding implode because the original return value of the src would be 'Array'. Now that I added the implode(), the src is the following:
<img src="1image.php?u=1&dateline=1295535226 width=" 120"="" height="26" "="" border="0" alt="" />
There's '1', which I'd assume is the (second) array element. And it also kind of messed up the entire img tag's attributes.
I guess I'm either doing this wrong, or the Array isn't supposed to be there (thus, $avauri)?
Also, much appreciated for taking your time to response. :)
Yeah, sorry - I should have looked at fetch_avatar_url before assuming I knew what it returns. It does return an array, and it seems like it either just has one array element with the url, or else it has a "hascustom" element set to 1, with a number of other fields. I looked for other places that call fetch_avatar_url, but I'm still not sure how it's all supposed to be used. Here's one from user.php (in admincp, I think):
require_once(DIR . '/includes/functions_user.php');
$userinfo['avatarurl'] = fetch_avatar_url($userinfo['userid']);
if ($userinfo['avatarurl'] == '' OR $userinfo['avatarid'] != 0)
{
$userinfo['avatarurl'] = '<img src="' . $vbulletin->options['cleargifurl'] . '" alt="" border="0" />';
}
else
{
$userinfo['avatarurl'] = "<img src=\"../" . $userinfo['avatarurl'][0] . "\" " . $userinfo['avatarurl'][1] . " alt=\"\" border=\"0\" />";
}
That code's kind of confusing because the return value is put in $userinfo['avatarurl'] and it's also using $userinfo['avatarid'] that comes from somewhere else, I guess.
It could be that fetch_avatar_url is only used in a few special places because it does a query to get the answer, so if you put it on a page with a lot of avatars it's going to add a lot of queries (which maybe doesn't matter to you, but just so you know..).
DataHero
01-25-2011, 09:52 AM
Hello,
My apologies for the slow reply. Been quite busy these few days. :)
Indeed, the return value of fetch_avatar_url() is set to 1 on both avatar URIs, so I'm assuming that the 1 is the result of 'hascustom', moreso than it being an actual array element identifier.
Oddly enough, using implode() for the variable $avauri (which is fetch_avatar_url()), would result in it actually giving its own width and height parameters, but moreso in a buggy way:
<img src="1image.php?u=2&dateline=1295528513 width=" 120"="" height="26" "=""
I've tried using ltrim() to get rid of the 1 in front of the image.php and str_ireplace() for the ones on the right side, but seeing as the values are rather dynamic, I doubt it'd help.
I'm really eager to get this working, for it'd be a great addition to my fourth 'mod'. :)
Also, I'd like to thank you once more for taking your time to assist me in this matter.
I suppose the values were returned that way because sometimes just the path is needed and sometimes the width and height are also needed.
Yes, I don't think it was meant to be imploded because of the hascustom thing in there. I guess you could either use
$aviuri[0] . ' ' . $aviuri[1]
or if you wanted to use implode do something like
$aviuri['hascustom'] = '';
first.
DataHero
01-25-2011, 10:37 AM
Hey kh99,
Thanks for the very quick response, much appreciated.
The second one ( $avauri['hascustom'] = ''; ) worked perfectly; the 1 prior to image.php is now gone, and the avatars are now fetched from the filesystem, and thus, update whenever I - or someone else - update the avatar.
The only issue that remains would be the width="" messed up, and extra given parameters as pasted in the code above. Do you recommend I simply get rid of that with a str_replace(), or would there be a more clever and efficient way to that?
Once more, thanks a fortune. :)
Just looking at the code for fetch_avatar_url() I can't see why that would happen. Maybe try the first way (from my previous post) of just concatenating the 0 and 1 elements and see what that looks like?
DataHero
01-25-2011, 10:54 AM
Unfortunately concatenating the elements did not change a bit in regards to that. Here's the current IMG tag (I've removed my manually set width and height from the template; it doesn't appear twice each now, but it is buggy still):
<img src="image.php?u=1&dateline=1295958757 width=" 120"="" height="68" "="" alt="Test's avatar" title="Test's avatar"/>
OK, well thanks for trying that anyway. I see now one problem with using implode: the $aviuri[0] part doesn't have quotes around it but the width/height does have its own quotes, so I guess instead of implode you'd need something like:
$url = "<img src=\"$avruri[0]\" $avruri[1] alt=\"Test's avatar\" title=\"Test's avatar\"/>";
DataHero
01-25-2011, 11:15 AM
I've tried as you suggested and the avatar doesn't appear at all now.
$url = "<img src=\"$avauri[0]\" $avauri[1] alt=\"$uname\'s Avatar\" title=\"$uname\'s Avatar\" />";
Tried switching [0] and [1] too, but to no avail.
EDIT:, apparently, now this appears as result:
<img src=" width=" 120"="" height="68" "="" image.php?u="1&dateline=1295958757" alt="Test\'s Avatar" title="Test\'s Avatar"/>
EDIT2: Alright, fixed! It was my bad, it did work in the first place; $avauri[0] returns the url, and $avauri[1] returns width and height. So, it works perfectly now.
Thanks a fortune, kh99! :D
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.