Participants variable?

16 Mar 2017
#1
Hello Shade,

I'm curious how you displayed the total number of thread participants. Is there a variable in MyBB to display the number?
Shade 16 Mar 2017
#2
I'm hooking into showthread_linear with the following function:

global $mybb$db$tid$participants;

$query $db->write_query("
    SELECT DISTINCT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar
    FROM " 
TABLE_PREFIX "users u
    LEFT JOIN " 
TABLE_PREFIX "posts p ON (p.uid = u.uid)
    WHERE p.tid = '
$tid'
    ORDER BY p.dateline
"
);

$plural '';
$num $db->num_rows($query);
if (
$num 1$plural 's';

$participants '<div class="participants"><h4>' $num ' participant' $plural '</h4><span>';

while (
$user $db->fetch_array($query)) {
    
$avatar "<img src='{$user['avatar']}' title='{$user['username']}' class='avatar' />";
    
$participants .= build_profile_link($avatar$user['uid']);
}

$participants .= '</span></div>'
The template is very ugly, but that just works fine in my case. I can then reference {$participants} into the showthread template. If you are wondering, I am using Hooks by frostschutz to apply this small enhancement.

In the future, quick replies will automatically add the current user avatar if not included.
Eldenroot, Brenda, Harry K. And 1 others like this post
Sharree 16 Mar 2017 Edited
#3
Wow thank you so much for sharing the code. I'll try it out.

One other think I want to ask. Would it also be possible to get the # of participants in the forumdisplay_thread
Shade 17 Mar 2017
#4
You will need to alter the hook and the query (at least for the $tid part, which is the one specifying the thread). However, I'd go with a different approach to reduce queries to the minimum. I might write down a tutorial for both cases as soon as I have some spare time.
Sharree 17 Mar 2017
#5
Very nice Shade, it worked perfectly. Thank you so much for sharing that.
Sharree 24 Mar 2017 Edited
#6
Shade would there be a way to limit the amount of avatars to output to around 10. I have several threads on my forum with over 600 participants and with all avatars displaying it would slow the load time. Thank you.
Shade 24 Mar 2017
#7
Add a LIMIT condition to the query, specifying how many elements should be returned.

$query $db->write_query("
    SELECT DISTINCT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar
    FROM " 
TABLE_PREFIX "users u
    LEFT JOIN " 
TABLE_PREFIX "posts p ON (p.uid = u.uid)
    WHERE p.tid = '
$tid'
    ORDER BY p.dateline
    LIMIT 10
"
); 
If you want to show the real number of participants alongside the last 10 participants, things are a bit more complex as you will need to write down a separate COUNT query.
Sharree 25 Mar 2017 Edited
#8
Thank you. That worked well. Here is the code I'm using if anyone wants to limit the avatars.

        global $mybb$db$tid$participants_num;

$query $db->write_query("
    SELECT DISTINCT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar
    FROM " 
TABLE_PREFIX "users u
    LEFT JOIN " 
TABLE_PREFIX "posts p ON (p.uid = u.uid)
    WHERE p.tid = '
$tid'
    ORDER BY p.dateline
"
); 

$plural '';
$num $db->num_rows($query);
if (
$num 1$plural 's';

$participants_num '<div class="participants">' $num ' participant' $plural '</div>';


        global 
$mybb$db$tid$participants;

$query $db->write_query("
    SELECT DISTINCT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar
    FROM " 
TABLE_PREFIX "users u
    LEFT JOIN " 
TABLE_PREFIX "posts p ON (p.uid = u.uid)
    WHERE p.tid = '
$tid'
    ORDER BY p.dateline
    LIMIT 10
"
); 

$participants '<div class="participants"><span>';

while (
$user $db->fetch_array($query)) {
    
$avatar "<img src='{$user['avatar']}' title='{$user['username']}' class='avatar'/>";
    
$participants .= build_profile_link($avatar$user['uid']);
}

$participants .= '</span></div>'
    } 
Use {$participants} for avatars and {$participants_num} for number of participants.
Shade 25 Mar 2017 Edited
#9
I'd rather do:

$query $db->simple_select('posts''COUNT(DISTINCT(uid)) AS count''tid = ' $tid);
$num $db->fetch_field($query'count'); 
Which is faster as you don't need to join multiple tables. I'd also subtract the 10 participants to show the last 10 with the avatar, and a label with "... and X more" or something like this.
Sasori likes this post
Brenda 7 May 2017
#10
global $mybb, $db, $tid, $participants;

$query = $db->write_query("
   SELECT DISTINCT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar
   FROM " . TABLE_PREFIX . "users u
   LEFT JOIN " . TABLE_PREFIX . "posts p ON (p.uid = u.uid)
   WHERE p.tid = '$tid'
   ORDER BY p.dateline
");

$plural = '';
$num = $db->num_rows($query);
if ($num > 1) $plural = 's';

$participants = '<div class="participants"><h5>' . $num . ' participant' . $plural . '</h5><span>';

while ($user = $db->fetch_array($query)) {
    $avatar = "<img src='{$user['avatar']}' title='{$user['username']}' class='avatar'/>";
    $participants .= build_profile_link($avatar, $user['uid']);
}



$participants .= '</span></div>';
I am using this, but once there are 30 participants, it gets over the "new reply" button. I don't like to limit, but i want to show all participants, whether the number is high or not.

How could i make it, so if the participants numbers hit 29, it breaks the line? See my screenshots:

How it looks good until 29:
[Image: J4kGmV4.png]

How it should continue after 29, number 30, 31, etc. should continue below the 1st one, without making a space line to the new reply button:
[Image: vmAODBZ.jpg]

Any idea anyone?