MyBB Community Forums

Full Version: Trying to display avatar of last poster
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For a theme I am working on I want to display the last posters avatar.

My approach here is to create a variable holding the link to the avatar. As those links are saved in the users table I added the following code to "forumdisplay.php" on line 1257 shortly after $lastposter and $lastposteruid are defined.
$query = $db->simple_select('users', 'avatar', "uid = $lastposteruid");
		$result = $db->fetch_array($query);
		$lastposter_avatar = $result['avatar'];

My assumption is that I can now use
<img src="{$lastposter_avatar}" class="poster-img">

in my template to display the last posters avatar.

This however does not work as I expect it to. How would I go about this?
And whats the best practice for adding variables?



During research I found a plugin for this but as I am changing the structure of the generated HTML I would rather code this in myself.
Yea i can't figure out how to make this code work either.
I've been at it for a while lol


The thing is, It wont even work if you feed the result raw data that all it has to do is output.
I took the input of an avatar from one of the row's in my database and tried to feed it as a result and it still didn't work so i don't even know if it's even checking and if so then i don't think it's checking right.
You'll have to assign the variable to be written to the template I assume.

Something like

$tpl->assign('avatar', $lastposter_avatar);

I don't know how the MyBB template engine works backend so look into it. Smile
After $lastposteruid (line 1258)

add

$user = get_user($lastposteruid);

       if (!empty($user['avatar']))
           {
               $lastposteravatar = ''.$user['avatar'].'';
           }
           else
           {
               $lastposteravatar = ''.$mybb->settings['useravatar'].'';
           }
and in forumdisplay template -> forumdisplay_thread place
<img src="{$lastposteravatar}" alt="#" class="lastposter_avatar" />
near last post info
The code above will result in 1 get_user() query per different last poster. 50 threads per page in UCP -> Edit Options = possibly 50 different last posters = 50 queries per page. And all of them select not only avatar column, but everything from the users table. Don't even try it on a big forum. Also, it doesn't use 1.8's format_avatar() function.

Here a plugin that always requires only 1 query per page: https://github.com/TommM/flpavatar/blob/...r.php#L163

And if you want to do it in core, you should simply select the avatar column in this query instead: https://github.com/mybb/mybb/blob/featur...y.php#L857
To access the variable in the template, you will also have to globalise it:

global $lastposter_avatar;
Thank you all for replying.

I managed to get it to work. After figuring out that "forumdisplay" was not even what i wanted to work on. I wanted to implement this for the index page. So editing "function_forumdisplay.php" on line 305 where the last post is managed got it to work. I added:

$uid = $lastpost_data['lastposteruid'];
$query = $db->query("SELECT avatar FROM mybb_users WHERE uid=$uid");
$result = $db->fetch_array($query);
$lastposter_avatar = $result['avatar'];


With that I am now able to reference "$lastposter_avatar" in my "forumbit_depth2_forum_lastpost" template.

Not quite sure how sophisticated this solution is, but it is working.
(2014-11-14, 01:21 PM)HoverBaum Wrote: [ -> ]Not quite sure how sophisticated this solution is, but it is working.

Just like the code above, not optimised at all. 100 forums on index page (doesn't even matter if the same lastposter or not this time) = 100 queries. Pain for the MySQL/pgSQL/SQLite server.