View Full Version : List members of additional usergroup
Spyike
07-19-2011, 09:24 PM
I am attempting to have a page which lists all users in a usergroup (additional usergrou). I realize I am able to do so via ACP, but I wish to have this list accessible to moderators.
I have the following code below, but am not sure where lies the error.. any help is appreciated.
<?php
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "Market Banned: $str";
?>
What happens, do you just get no user names? Is 43 a secondary user group, by any chance? If so I think you'd want
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)
Spyike
07-19-2011, 10:09 PM
What happens, do you just get no user names? Is 43 a secondary user group, by any chance? If so I think you'd want
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)
It is an additional usergroup, the error I receive is:
Fatal error: Call to a member function query_read() on a non-object in mban.php on line 3
Will report back with your edit.
Edit:
<?php
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids);
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "Market Banned: $str";
?>
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in mban.php on line 9
The error you were getting is because the db object isn't defined, which usually is because $vbulletin isn't in scope. The new error is because you're missing a close quote on the query string. Try this:
<?php
global $vbulletin;
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)";
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "Market Banned: $str";
?>
That assumes that what you posted is only part of a page (like it's included or it's plugin code or something). If you're trying to make a stand-alone page then you need to inlucde global.php. (If you are trying to make a page you might want to look at this: https://vborg.vbsupport.ru/showthread.php?t=228112).
Spyike
07-19-2011, 10:52 PM
The error you were getting is because the db object isn't defined, which usually is because $vbulletin isn't in scope. The new error is because you're missing a close quote on the query string. Try this:
<?php
global $vbulletin;
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)";
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "Market Banned: $str";
?>
That assumes that what you posted is only part of a page (like it's included or it's plugin code or something). If you're trying to make a stand-alone page then you need to inlucde global.php. (If you are trying to make a page you might want to look at this: https://vborg.vbsupport.ru/showthread.php?t=228112).
I am just looking to have a page that displays those users so moderators can have it readily available if needed. I have created the template and page as shown in the tutorial you linked; however, I am not sure where to place the original code snipped you posted above as I cannot put it in the template itself. Would I need to create a plugin and include that in the template? If so, what hook do I need to use and what would I do?
Thanks
You should be able to take this part of the code:
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)";
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
and put it where the tutorial says "// ###### YOUR CUSTOM CODE GOES HERE #####". Then instead of using echo, you'd probably want to register $str as a variable in your template like:
$templater->register('str', $str);
(you can see where these go in the example code), and then use it in a tag in the template like {vb:raw str}.
Spyike
07-19-2011, 11:26 PM
You should be able to take this part of the code:
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)";
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
and put it where the tutorial says "// ###### YOUR CUSTOM CODE GOES HERE #####". Then instead of using echo, you'd probably want to register $str as a variable in your template like:
$templater->register('str', $str);
(you can see where these go in the example code), and then use it in a tag in the template like {vb:raw str}.
Now getting a syntax error :/
marketbanned.php contents vvv
<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'mban');
define('CSRF_PROTECTION', true);
// change this depending on your filename
// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();
// get special data templates from the datastore
$specialtemplates = array();
// pre-cache templates used by all actions
$globaltemplates = array('mbanned',
);
// pre-cache templates used by specific actions
$actiontemplates = array();
// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');
// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################
$navbits = construct_navbits(array('' => 'Market Banned Users'));
$navbar = render_navbar_template($navbits);
// ###### YOUR CUSTOM CODE GOES HERE #####
$pagetitle = 'Market Banned Users';
$groupid = 43;
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)";
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
$templater->register('str', $str);
// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######
$templater = vB_Template::create('mbanned');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());
?>
mbanned template contents vv
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
<head>
<title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
{vb:raw headinclude}
{vb:raw headinclude_bottom}
</head>
<body>
{vb:raw header}
{vb:raw navbar}
<div id="pagetitle">
<h1>{vb:raw pagetitle}</h1>
</div>
<h2 class="blockhead">Title</h2>
<div class="blockbody">
<div class="blockrow">
{vb:raw str}.
</div>
</div>
{vb:raw footer}
</body>
</html>
My fault, there's a missing ')' on the query line so it should be
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)");
also, you want to move your $templater->register('str', $str) down to where the other $templater->register() lines are.
Spyike
07-20-2011, 01:20 AM
My fault, there's a missing ')' on the query line so it should be
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)");
also, you want to move your $templater->register('str', $str) down to where the other $templater->register() lines are.
Worked fabulously! Thank you!
Lastly, instead of seperating these users with commas , how would I go about doing so with line breaks rather than ' , ' ? I also attempted to add a conditional to the template to only display the str output to certain usergroups, but it was unsuccessful. Would the condition need to be done in the PHP file rather than in the template?
Thanks again for all your help
HMBeaty
07-20-2011, 01:22 AM
Worked fabulously! Thank you!
Lastly, instead of seperating these users with commas , how would I go about doing so with line breaks rather than ' , ' ? I also attempted to add a conditional to the template to only display the str output to certain usergroups, but it was unsuccessful. Would the condition need to be done in the PHP file rather than in the template?
Thanks again for all your help
Pretty sure using
<br />
would work
--------------- Added 1311128579 at 1311128579 ---------------
so just change
$sep = ', ';
to
$sep = '<br />';
Spyike
07-20-2011, 01:34 AM
Pretty sure using
<br />
would work
--------------- Added 1311128579 at 1311128579 ---------------
so just change
$sep = ', ';
to
$sep = '<br />';
Worked like a charm, thanks. Any idea for font size?
Worked fabulously! I also attempted to add a conditional to the template to only display the str output to certain usergroups, but it was unsuccessful. Would the condition need to be done in the PHP file rather than in the template?
You could do it in the php or the template. If that's the only thing on the page, you could protect the whole page with:
if (!is_member_of($vbulletin->userinfo, 5, 6, 7))
{
print_no_permission();
}
(of course you can list whichever user groups you want to have access to the page). If you'd rather leave the page accessible to everyone but just hide something in the template, you could use:
<vb:if condition="is_member_of($bbuserinfo, 5, 6, 7)">
Secret stuff
</vb:if>
Spyike
07-20-2011, 02:30 AM
You could do it in the php or the template. If that's the only thing on the page, you could protect the whole page with:
if (!is_member_of($vbulletin->userinfo, 5, 6, 7))
{
print_no_permission();
}
(of course you can list whichever user groups you want to have access to the page). If you'd rather leave the page accessible to everyone but just hide something in the template, you could use:
<vb:if condition="is_member_of($bbuserinfo, 5, 6, 7)">
Secret stuff
</vb:if>
The template conditional did not work, that is why I inquired about using PHP conditional. Thanks again for all the help.
Edit: I do find it odd that on the custom page, despite already being logged in vB, it shows the login box for user name and password rather than seeing the typical settings , logout etc that you would if being logged in.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.