MyBB Community Forums

Full Version: Database Table Joins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Of all the queries I created I can't seem to join the users and usergroup tables so that I can output a result within a while loop. I need to output the username and usergroup title (Not the usergroup ID) etc..... within the while loop.

The users table has a usergroup field but that only has the ID, I need the group title.

Anyone got any ideas, because this has me stumped Huh
Looks like I'm not the only one thats stumped lol Big Grin

No one got any ideas or other suggestions on this ?
Usergroups is stored in cache. You define $cache as global and try something like this:

//Get usergroup info from cache

$groups = $cache->read('usergroups');
$usertitle = $groups[$mybb->user['usergroup']]['title']

And this will also save you queries.
(2013-01-14, 03:54 PM)crazy4cs Wrote: [ -> ]Usergroups is stored in cache. You define $cache as global and try something like this:

//Get usergroup info from cache

$groups = $cache->read('usergroups');
$usertitle = $groups[$mybb->user['usergroup']]['title']

And this will also save you queries.

No,no,no,no,no,no,no and no.

Ive already posted in the thread where you got that from and thats not what I need. That outputs the usergroup of the current user, not what I want. For example, if you where viewing my forum that will output "your" usergroup. Ive stated in my original post that I'm using a "while loop".

Inside the while loop it retrieves the users, username and usergroup ID. Im trying to convert the ID to that users, usergroup title. So now, if your looking at my forum and you're looking at "Tom" for example, you'll see his usergroup title, not yours.
(2013-01-14, 04:13 PM)Frank.Barry Wrote: [ -> ]
(2013-01-14, 03:54 PM)crazy4cs Wrote: [ -> ]Usergroups is stored in cache. You define $cache as global and try something like this:

//Get usergroup info from cache

$groups = $cache->read('usergroups');
$usertitle = $groups[$mybb->user['usergroup']]['title']

And this will also save you queries.

No,no,no,no,no,no,no and no.

Ive already posted in the thread where you got that from and thats not what I need. That outputs the usergroup of the current user, not what I want. For example, if you where viewing my forum that will output "your" usergroup. Ive stated in my original post that I'm using a "while loop".

Inside the while loop it retrieves the users, username and usergroup ID. Im trying to convert the ID to that users, usergroup title. So now, if your looking at my forum and you're looking at "Tom" for example, you'll see his usergroup title, not yours.
Not sure what you meant by "where I got this from". This is very basic stuff and ones with experience in MyBB can point out this code instantly.

And regarding your second doubt, it was an example that I showed. If you need the info of say user "x", then you go on some path like this:

//Assuming we are viewing a user's profile, we start from getting his uid

$uid = intval($mybb->input['uid']); //You can also use $memprofile['uid'] but define $memprofile as globals ($memprofile only works when viewing a profile)

//We now get user's info from the uid

$user = get_user($uid);

//Read groups

$groups = $cache->read('usergroups');

//Now we get the input user's usertitle

$usertitle = $groups[$user['usergroup']]['title']

Adjust accordingly.
since youa re worried about what to show, I assume you want the displaygroup instead of the main usergroup or any additionalgroups?


1) load usergroups cache via
$groups = $cache->read('usergroups');
2) perform query on users table to get info you need, including usergroup, additionalgroups and displaygroup. no need to join tables
3) iterate over the query results
4) in each iteration, use above code for pulling the group info
$grouptitle = $groups[$user['usergroup']]['title'];

or if you want the displaygroup
if($user['displaygroup'])
{
  $grouptitle = $groups[$user['displaygroup']]['title'];
}
else
{
  $grouptitle = $groups[$user['usergroup']]['title'];
}
and if you want additionalgroups then you need to loop over those and append them

$addgroups = explode(',', $user['additionalgroups']);
$grouptitle = $groups[$user['usergroup']]['title'];;
foreach($addgroups as $gid => $group)
{
  $grouptitle .= '<br />'; //format/layout to your needs
  $grouptitle .= $groups[$gid]['title'];
}
(2013-01-14, 04:45 PM)pavemen Wrote: [ -> ]since youa re worried about what to show, I assume you want the displaygroup instead of the main usergroup or any additionalgroups?


1) load usergroups cache via
$groups = $cache->read('usergroups');
2) perform query on users table to get info you need, including usergroup, additionalgroups and displaygroup. no need to join tables
3) iterate over the query results
4) in each iteration, use above code for pulling the group info
$grouptitle = $groups[$user['usergroup']]['title'];

or if you want the displaygroup
if($user['displaygroup'])
{
  $grouptitle = $groups[$user['displaygroup']]['title'];
}
else
{
  $grouptitle = $groups[$user['usergroup']]['title'];
}
and if you want additionalgroups then you need to loop over those and append them

$addgroups = explode(',', $user['additionalgroups']);
$grouptitle = $groups[$user['usergroup']]['title'];;
foreach($addgroups as $gid => $group)
{
  $grouptitle .= '<br />'; //format/layout to your needs
  $grouptitle .= $groups[$gid]['title'];
}

Thats exactly what I need. +1 paveman Big Grin