How do I display User Avatars on custom pages? - code help - SMF 2.0

Started by ElectricSquid, October 21, 2009, 05:16:33 PM

Previous topic - Next topic

ElectricSquid

Hi all, I'm building a custom mod for SMF 2.0 and I need a little help on this one small part.

I'm trying to show the users avatar next to their post (in my custom mod) and I'm having trouble finding where in the database this info is kept, and/or what code to use to make the avatar show, per user's post.

I know there's a database row called 'avatar' in the members table, but it only shows the name of the stock avatar (ie. stock_avatar.jpg). This database row does not give any info on user uploaded or remote location avatars.

I've looked all over SMF 2.0 for this info I need, but I'm drawing a blank.
It seems like this info is much different than SMF 1.1.x

Anyway, any help would be appreciated.


Please be advised that this info will be used for a mod that will be offered free on the Mod Site.
If you offer code, please allow me permission to use it in this mod :)
Thanks!!

Arantor

There are 3 kinds of avatars.

1. Stock avatars, stored in the avatar column of the members table, as relative path.
2. External avatar (not downloaded to server), stored in avatar column of the members table, as full URL.
3. Uploaded avatar, stored primarily in the attachments table.

Thing to do is to call loadMemberData with the user id you want to get, which will populate 'avatar' and 'filename' in $user_profile[$id] where $id is the user id number, e.g. $user_profile[1] for user 1.

Then you can just evaluate the two fields yourself to figure which type of avatar it is - ['filename'] will cover 3, ['avatar'] will cover 1 and 2.
Holder of controversial views, all of which my own.


ElectricSquid

#2
Thank you!!
That worked.

ElectricSquid

#3
I take that back :P It almost worked.
I did what you said, but didn't quite get it.
So I used "loadMemberData" as a search term (thank you :D) and I found this code that does just about what you said to do. It works. It shows every avatar on the page...

...BUT, and a big butt :P
It creates 128 queries in order to do it's job, way too many for a functioning mod.
I think the issue is it's a foreach working in a foreach.
My problem is, I don't know how to code it another way that works.

Every time I try, it stops working.

Please show me how this is done properly




// Get our users
$UsersID = array(1);

// Get it all working.
$members = loadMemberData($UsersID, false, 'normal');

// Output.. That was simple..
foreach($members as $p)
{
   loadMemberContext($p);
   echo $memberContext[$p]['avatar']['image'];
}




This is the function this will be used in.
The $UsersID gets it's info from $mod['poster']['id']



function template_main()

{

global $context, $txt, $message, $memberContext, $user_profile;

loadLanguage('Mod');



foreach ($context['mod_post_data'] as $mod)
{

if ($mod['id_msg'] == $mod['id_first_msg'])
{
echo '<div style="padding:20px; border: solid 2px #ff0000;">';
}

else
{
echo '<div style="padding:20px; border: solid 2px #000000;">';
}

echo '
<div class="smaller">', $mod['poster']['link'], ' ', $txt['at'], ' ', $mod['time'], '</div>
<div class="post" style="padding: 2ex 0;">', $mod['body'], '</div>
<div class="smaller">', $mod['comment_link'], '</div>
</div>';

}

}




Arantor

Quote from: ∑£ℓ¢†®¡¢ §ợų¡đ on October 21, 2009, 10:13:33 PM
...BUT, and a big butt :P
It creates 128 queries in order to do it's job, way too many for a functioning mod.

Do what Display.php does for a thread: figure out all the users you need details for, then call it once with an array of user ids.
Holder of controversial views, all of which my own.


ElectricSquid

The part that has me beat is where it says...


foreach($members as $p)
{
   loadMemberContext($p);
   echo $memberContext[$p]['avatar']['image'];
}



How do I recode this foreach to do it the way you suggest?

ElectricSquid

Yea, this one has me beat >:(
I've tried everything I can think of, from splitting it off individually, to adding it to the main array coming out of Sources/xxx.php, where it's getting it's info straight from $row['id_user'].

I need someone to actually show me how this is done, this one's kicking my @$$ :(

Kays

loadMemberContext will accept an array. So you do it something like this:


loadMemberContext($members);

foreach ($members as $p)
   echo $memberContext[$p]['avatar']['image'];

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

ElectricSquid

Quote from: Kays on October 21, 2009, 11:44:06 PM
loadMemberContext will accept an array. So you do it something like this:


loadMemberContext($members);

foreach ($members as $p)
   echo $memberContext[$p]['avatar']['image'];


So loadMemberContext($members) doesn't need to be inside the foreach?
That's helpful!!

But what about the foreach in that code?
I have a situation where I am trying to add this into a big foreach that fills the page with posts (much like Display does). Is there a way to add $members as $p to the existing foreach?
Like...


foreach ($context['mod_post_data'] as $mod && $members as $p)




ElectricSquid

or is there a way to recode echo $memberContext[$p]['avatar']['image']; so it wasn't an echo?
Maybe then I could add that is where I need it, without the rest of the foreach having to be inside my existing foreach.

Or does that not matter?

Kays

I don't think it will work like that since the arrays are of diferent sizes.

As Arantor suggested, look in Display.php to see how its done there. As the messages are pulled, a seperate array of the poster ids is created. Then loadMemberContext is called with those ids, before the message array is built. Since each message has a poster id, that is used to pull that member's data.

What you'll need to do is to associate each member's id with the message $context['mod_post_data']['member']

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Arantor

Don't forget you have to call loadMemberData before calling loadMemberContext...
Holder of controversial views, all of which my own.


ElectricSquid

I'm curious why it was made so difficult to show a simple avatar image, while it's so easy to load pretty much any of the other member info?

I looked through Display.php to see how it was done there. The code references other code, again and again, to the point that it's almost impossible to follow. That's why I posted here, because I tried that route and got to a point where it lost me.

The display.php route references code in load.php, where I believe the avatar info is originally pulled from god knows where it's getting it. BUT, along with loadMemberContext, comes a monstrous amount of info that I don't need because I already have it pulled from the database (as mentioned above in my opening sentence).

I apologize if this just sounds like me complaining, but there is a reason. All I need is the avatar per user, not the huge memory load that comes with getting it the way/array suggested. Is there another way?

sangwe11

Quote from: ∑£ℓ¢†®¡¢ §ợų¡đ on October 22, 2009, 11:07:14 AM
I'm curious why it was made so difficult to show a simple avatar image, while it's so easy to load pretty much any of the other member info?

I looked through Display.php to see how it was done there. The code references other code, again and again, to the point that it's almost impossible to follow. That's why I posted here, because I tried that route and got to a point where it lost me.

The display.php route references code in load.php, where I believe the avatar info is originally pulled from god knows where it's getting it. BUT, along with loadMemberContext, comes a monstrous amount of info that I don't need because I already have it pulled from the database (as mentioned above in my opening sentence).

I apologize if this just sounds like me complaining, but there is a reason. All I need is the avatar per user, not the huge memory load that comes with getting it the way/array suggested. Is there another way?

Its not so difficult, its because your making it thread through all the users (which seems to be 128 or something) and it does the query for every one, resulting in 128 queries.

The best thing to do, is make an array of JUST the users info you need, then do the loadmemberdata function using that array, it will return an array of thoses members, containing an array all all the info.

Then foreach that array, and echo the users avatar !

Kays

QuoteI'm curious why it was made so difficult to show a simple avatar image, while it's so easy to load pretty much any of the other member info?

It's not that difficult. I think part of the problem right now is that you are trying to add this directly to the template file. When it should be added to the $context['mod_post_data'] array when it is being built.

Can you post that code so we can see how you are putting that together?

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Arantor

Quote from: ∑£ℓ¢†®¡¢ §ợų¡đ on October 22, 2009, 11:07:14 AM
I'm curious why it was made so difficult to show a simple avatar image, while it's so easy to load pretty much any of the other member info?

Because the required information is in two different places (two different tables at work), potentially, and served from any one of four different possible URLs.

Display.php gets the list of messages, iterates through them and builds a list of posters. It then calls loadMemberData on that array.

You don't need loadMemberContext as well; I do it just fine without calling loadMemberContext in my own code because I just don't need all that.

If you get data from the user, do it all through loadMemberData. That way if things change, your code won't have to.

If you really have to do it yourself, look at what loadMemberData does given 'normal' or 'profile' mode. Essentially you query the members table LEFT JOIN attachments table, and check if null in the latter.


Speaking from the Customizer Team: any query that gathers data from the database inside a template is a no-go, we will not approve it for the mod site.
Holder of controversial views, all of which my own.


ElectricSquid

Quote from: Kays on October 22, 2009, 11:42:39 AM
QuoteI'm curious why it was made so difficult to show a simple avatar image, while it's so easy to load pretty much any of the other member info?

It's not that difficult. I think part of the problem right now is that you are trying to add this directly to the template file. When it should be added to the $context['mod_post_data'] array when it is being built.

Can you post that code so we can see how you are putting that together?

I'm not adding it to the template file, I am trying (failing) to add it to the Sources/xxxx file.
I sent you a PM with the code so far, since the last time I posted code turned into a bad day.

Kays


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Advertisement: