MyBB Community Forums

Full Version: Getting Username from Award UID
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
$user = get_user(1); // 1 = UID

$username = format_name($user['username'], $user['usergroup'], $user['displaygroup']);

The code that is commonly used to get in this case the username of UID 1.

I've tried this and had no success:

$user = get_user($user['awuid']);

$username = format_name($user['username'], $user['usergroup'], $user['displaygroup']);

Note I have already call the myawards_user table from my database. That part is working fine.

It doesn't appear to work. Help? Confused
Could we see the rest of the code please just so we can see what $user['uid'] should be?
(2013-06-07, 04:07 PM)Euan T Wrote: [ -> ]Could we see the rest of the code please just so we can see what $user['uid'] should be?

[Image: 3392fcfb1637690252ec0286b5d9880b.png]

} elseif($mybb->input['action'] == 'awarded') {
		$page .= "<tr><td class=\"thead\" colspan=\"3\">Recently Awarded Users</td></tr>
<tr><td class=\"tcat smalltext\" align=\"center\"><strong>Username</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Award Icon</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Reason</strong></td></tr>";

	$query = $db->query("SELECT * FROM ".TABLE_PREFIX."myawards_users ORDER BY awutime DESC LIMIT 25");
	
	$user = get_user($user['awuid']);
	$username = format_name($user['username'], $user['usergroup'], $user['displaygroup']); 
	
	while($user = $db->fetch_array($query))
	{
		$page .= "<tr><td class=\"trow1\" width=\"20%\" align=\"center\">".$username."</td>
				  <td class=\"trow1\" width=\"20%\" align=\"center\">".$user['awid']."</td>
				  <td class=\"trow1\" width=\"60%\" align=\"center\">".$user['awreason']."</td></tr>";
	}

	$page .= "</table><table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\" style=\"width: 20%\">
		<tbody><tr><td class=\"thead\">Menu</td></tr>
{$links}
</tbody></table>";
Use you this code inside the while loop;
$user = get_user($user['awuid']);
    $username = format_name($user['username'], $user['usergroup'], $user['displaygroup']); 
$user isn't defined, which will be why it isn't working. Try this:

} elseif($mybb->input['action'] == 'awarded') {
	$page .= "<tr><td class=\"thead\" colspan=\"3\">Recently Awarded Users</td></tr>
<tr><td class=\"tcat smalltext\" align=\"center\"><strong>Username</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Award Icon</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Reason</strong></td></tr>";

	$query = $db->query("SELECT * FROM ".TABLE_PREFIX."myawards_users ORDER BY awutime DESC LIMIT 25");
	
	if ($db->num_rows($query) > 0) {
		while($user = $db->fetch_array($query)) {
			$userDetails = get_user((int) $user['awuid']);
			$username = format_name($userDetails['username'], $userDetails['usergroup'], $userDetails['displaygroup']); 
			$page .= "<tr><td class=\"trow1\" width=\"20%\" align=\"center\">".$username."</td>
					  <td class=\"trow1\" width=\"20%\" align=\"center\">".$user['awid']."</td>
					  <td class=\"trow1\" width=\"60%\" align=\"center\">".$user['awreason']."</td></tr>";
		}
	}

	$page .= "</table><table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\" style=\"width: 20%\">
		<tbody><tr><td class=\"thead\">Menu</td></tr>
{$links}
</tbody></table>"; 
(2013-06-07, 04:13 PM)Yaldaram Wrote: [ -> ]Use you this code inside the while loop;
$user = get_user($user['awuid']);
    $username = format_name($user['username'], $user['usergroup'], $user['displaygroup']); 

Dayum, worked perfectly. Thanks Yaldaram! Smile

(2013-06-07, 04:14 PM)Euan T Wrote: [ -> ]$user isn't defined, which will be why it isn't working. Try this:

} elseif($mybb->input['action'] == 'awarded') {
	$page .= "<tr><td class=\"thead\" colspan=\"3\">Recently Awarded Users</td></tr>
<tr><td class=\"tcat smalltext\" align=\"center\"><strong>Username</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Award Icon</strong></td><td class=\"tcat smalltext\" align=\"center\"><strong>Reason</strong></td></tr>";

	$query = $db->query("SELECT * FROM ".TABLE_PREFIX."myawards_users ORDER BY awutime DESC LIMIT 25");
	
	if ($db->num_rows($query) > 0) {
		while($user = $db->fetch_array($query)) {
			$userDetails = get_user((int) $user['awuid']);
			$username = format_name($userDetails['username'], $userDetails['usergroup'], $userDetails['displaygroup']); 
			$page .= "<tr><td class=\"trow1\" width=\"20%\" align=\"center\">".$username."</td>
					  <td class=\"trow1\" width=\"20%\" align=\"center\">".$user['awid']."</td>
					  <td class=\"trow1\" width=\"60%\" align=\"center\">".$user['awreason']."</td></tr>";
		}
	}

	$page .= "</table><table border=\"0\" cellspacing=\"{$theme['borderwidth']}\" cellpadding=\"{$theme['tablespace']}\" class=\"tborder\" style=\"width: 20%\">
		<tbody><tr><td class=\"thead\">Menu</td></tr>
{$links}
</tbody></table>"; 

Yours actually worked better. Thanks a lot, Euan. Smile
Also, it's a good idea to use htmlspecialchars_uni() when outputting raw data from the database like that to protect against possible XSS attacks.