MyBB Community Forums

Full Version: Display Avatar next to my news post query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I've come up with some code that is working as intended for my websites news/index page and I'd really like to add some flare to it. Basically my idea is that I'd like to call/fetch and display the avatar of the poster. I'll likely add some more HTML/CSS to make it look prettier but for now functionality is more important to me.

Long story short: I want to call my last posters avatar and display it. I've found inside the database where the avatar url is stored (mybb_users) but I can't for the life of me work out the logic in which to display it properly inside my while loop. Any and all help would be much appreciated.

Here is the code I have so far!
<?php
chdir("./forum/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
global $mybb, $db, $cache, $lang, $plugins;  // Trim this list of global vars, if you want to.
?>

<?php
$query = $db->query("
    SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message
    FROM ".mybb_."threads t
    INNER JOIN ".mybb_."posts p ON (p.tid=t.tid)
    WHERE t.fid='3' AND t.visible='1'
    ORDER BY t.dateline DESC
    LIMIT 6"// Change the last digit to how many recent post you want to be shown

); 
$list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"forum/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
$list .= "".htmlspecialchars_uni($fetch['message'])."<br>";
        $poster = "<a href=\"forum/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Posted by: {$poster}<br /><br />";
    
    }
    //output
    echo $list;
?>
Bump Sad
Add in your query:

LEFT JOIN ".TABLE_PREFIX."users lp ON (t.lastposteruid=lp.uid)

And in the select statement:

lp.avatar AS lpavatar

Making your query:

$query = $db->query("
	SELECT t.tid, t.fid, t.uid, t.username, t.subject, t.replies, t.views, t.lastposter, p.message, lp.avatar AS lpavatar
	FROM ".mybb_."threads t
	INNER JOIN ".mybb_."posts p ON (p.tid=t.tid)
	LEFT JOIN ".TABLE_PREFIX."users lp ON (t.lastposteruid=lp.uid)
	WHERE t.fid='3' AND t.visible='1'
	ORDER BY t.dateline DESC
	LIMIT 6"// Change the last digit to how many recent post you want to be shown
);

And to take the avatar, add in the while loop:

$lpavatar = format_avatar($fetch['lpavatar']);

To show it:

<img src="{$lpavatar['image']}" />



If you want to get also the dimensions of the avatar, add in the SELECT statement:

lp.avatardimensions AS lpavatardimensions

And to take the avatar:

$lpavatar = format_avatar($fetch['lpavatar'], $fetch['lpavatardimensions']);

To show it:

<img src="{$lpavatar['image']}" {$lpavatar['width_height']} />
Works perfectly, thank you!