PDA

View Full Version : Querying Usergroups Then Array Help?


paul41598
04-07-2009, 09:23 PM
I'm like really still newb when it comes to this stuff. Essentially I'm trying to pull all usergroups someone is in, primary and secondary. Then I want to display checkboxes for each group with the usergroup title next to each checkbox. So if a user is apart of 2 groups, then 2 checkboxes will appear with each usergroup name.

I'm thinking my theory is correct. I'm querying the database, then I get somewhat stumped. Won't I want to somehow put the ID's I get back in an array, and then use the array for the checkbox? Any pointers from here on out?



$users = $vbulletin->db->query_read("
SELECT user.userid, user.usergroupid, user.membergroupids, usergroup.usergroupid, usergroup.title
FROM " . TABLE_PREFIX . "user AS user
LEFT JOIN " . TABLE_PREFIX . "usergroup AS usergroup ON(usergroup.usergroupid = user.usergroupid)
WHERE user.userid = ".$vbulletin->userinfo['userid']."
");

while ($user1 = $vbulletin->db->fetch_array($users))
{

//checkbox stuff here
like $variable = '<input type="checkbox"> blah blah?
}

Dismounted
04-08-2009, 06:22 AM
You don't need to "re-fetch" the usergroup ids, it's already there...
$usergroupids = $vbulletin->userinfo['usergroupid'];

if (!empty($vbulletin->userinfo['membergroupids']))
{
$usergroupids .= ',' . $vbulletin->userinfo['membergroupids'];
}

$usergroups = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "usergroup
WHERE usergroupid IN ($usergroupids)
");

while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
// some stuff
}

paul41598
04-08-2009, 09:23 AM
Ahh, that makes sense. So I can echo back $usergroupids, and I get all of the id's but what about the title of the usergroup? IF in the while loop I do a $title = $usergroup['title']; and put that $title variable in the newthread template, I only get 1 title to show up, even tho a user is apart of 3 groups. I was looking to display all the usergroup titles they are apart of.

Actually I need a checkbox associated to each user title. Would I need a for each loop? Thanks again dismounted

Dismounted
04-08-2009, 09:29 AM
$usergroupdata = array();
while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
$usergroupdata[$usergroup['usergroupid']] = $usergroup['title'];
}

print_r($usergroupdata);

paul41598
04-08-2009, 11:03 AM
Ok I see, so that puts their usergroups in an array. Now I'd have to break out the array from there it sounds like. Thanks dismounted. Feel free to PM me if you have free time.

Dismounted
04-08-2009, 11:06 AM
I'm just demonstrating how it could be used. You don't have to store it inside an array, and then loop that array. Just generate template data inside the loop.

paul41598
04-08-2009, 11:13 AM
Well I tried this:

outside the while loop

foreach ($usergroupdata AS $test) {
eval('$der .= "' . fetch_template('jon') . '";');
}

and inside that template I put the variable $test

and it prints out all the usergroup titles, so it worked! However your saying it could be done another way right? Which is best?

--------------- Added 1239220692 at 1239220692 ---------------

Ok got a little further but stuck again. :(

newthread form complete hook:

$usergroupids = $vbulletin->userinfo['usergroupid'];

if (!empty($vbulletin->userinfo['membergroupids']))
{
$usergroupids .= ',' . $vbulletin->userinfo['membergroupids'];
}

$usergroups = $vbulletin->db->query_read("
SELECT *
FROM " . TABLE_PREFIX . "usergroup
WHERE usergroupid IN ($usergroupids)
");

$usergroupdata = array();
while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
$usergroupdata[$usergroup['usergroupid']] = $usergroup['title'];
}

foreach ($usergroupdata AS $value1 => $value) {
eval('$der .= "' . fetch_template('jon') . '";');
}

Templete Jon:

<label for="usergroups">
<input type="checkbox" name="value1[]" value="$value1" id="" tabindex="1" />$value
</label>



newthread post complete hook:

$vbulletin->input->clean_array_gpc('p', array('value1' => TYPE_ARRAY));

echo $vbulletin->GPC['value1'];

Granted theres supposed to be more code in the post complete hook, but the theory at least for now is just echo back whichever checkbox was selected. However value1 isn't doing me any good!

Dismounted
04-09-2009, 04:13 AM
However your saying it could be done another way right? Which is best?
What you're doing is using two loops, when you can just use one. Just place the template eval() into the while loop, removing the second unnecessary foreach() loop.

paul41598
04-09-2009, 09:20 AM
Ok I'll try that....thank you dismounted

Although when I did that, now it only shows 1 usergroup, as opposed to the 3 that Im apart of. So I think that foreach loop was needed.

Dismounted
04-09-2009, 11:50 AM
Make sure you have changed the variables in the template... (Also post the code you are using now.)

paul41598
04-09-2009, 12:15 PM
Right I did. In newthread template I have $der2 for example.

Then this is part of the code from earlier.
$usergroupdata = array();
while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
$usergroupdata[$usergroup['usergroupid']] = $usergroup['title'];
$der2 = $usergroup['title'];
eval('$der .= "' . fetch_template('jon') . '";');
}

But see I need the title and ID from each usergroup. The ID would get associated with the checkbox name or value I'm guessing. Then the usergroup title would just be listed to each checkbox

oh and the template jon I have:
<label for="usergroups">
<input type="checkbox" name="" value="" id="" tabindex="1" />$der2
</label>

--------------- Added 1239283376 at 1239283376 ---------------



Hmm, changed it around cuz I found some issues with my code (idiot) doh.

newthread form complete hook

$usergroupdata = array();
while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
//Assign group title to $grouptitle variable
$usergroupdata[$usergroup['title']] = $usergroup['title'];
$grouptitle = $usergroupdata[$usergroup['title']];

//Assign group ID to $groupID variable
$usergroupdata[$usergroup['usergroupid']] = $usergroup['usergroupid'];
$groupID= $usergroupdata[$usergroup['usergroupid']];

eval('$der .= "' . fetch_template('jon') . '";');
}


Jon template:
<label for="usergroups">
<input type="checkbox" name="groupID" value="$groupID" id="" tabindex="1" />$grouptitle
</label>

newthread post complete hook
$vbulletin->input->clean_array_gpc('p', array('groupID' => TYPE_ARRAY));

echo $vbulletin->GPC['groupID'];

At least its displaying so far.
http://www.thecentralword.com/misc/for_kotik/checkbox_usergroups.jpg

--------------- Added 1239285415 at 1239285415 ---------------

I think I got it to display all ID's that I check!

My newthread post complete hook was wrong. This works. Maybe you can double check to make sure its written correctly though. Also, what if I needed to get all email address from users associated with each checkbox. Wouldnt' I just do a left join on that original query?

$vbulletin->input->clean_array_gpc('p', array('groupID' => TYPE_ARRAY));

foreach ($vbulletin->GPC['groupID'] AS $id)
{
echo $id . "&nbsp;";
}

Dismounted
04-09-2009, 02:16 PM
You're making it so complicated...
while ($usergroup = $vbulletin->db->fetch_array($usergroups))
{
eval('$der .= "' . fetch_template('jon') . '";');
}
<label for="usergroups">
<input type="checkbox" name="groupID" value="$usergroup[usergroupid]" id="" tabindex="1" />$usergroup[title]
</label>

paul41598
04-09-2009, 02:33 PM
lol, ya I always do it seems :rolleyes:

So if I'm trying to pull all email address who are IN those $usergroupids

$usergroups = $vbulletin->db->query_read("
SELECT usergroup.usergroupid, usergroup.title, user.email
FROM " . TABLE_PREFIX . "usergroup AS usergroup, user AS user
WHERE usergroup.usergroupid IN ($usergroupids)

");

I tried the LEFT join on the user table, no good. Can you point me in the right direction of what I'm missing here?

UPDATE: ( 9:33pm)
Ok I've been working on this. Instead of the query going in the form complete, I'm putting it in there newthread post complete: Here is my updated code. It wont work though.

$vbulletin->input->clean_array_gpc('p', array('groupID' => TYPE_ARRAY));

foreach ($vbulletin->GPC['groupID'] AS $id)
{
$ids = $id;
}

$usergroups2 = $vbulletin->db->query_read("
SELECT email
FROM " . TABLE_PREFIX . "user
WHERE usergroupid IN ($ids)
");

while ($usergroups3 = $vbulletin->db->fetch_array($usergroups2))
{
//send to my email to test if subject line is parsing variables
vbmail("my_email_was_here.net", $usergroups3['email'], $message);
}

DJ29Joesph
04-09-2009, 05:51 PM
Funny, I am kinda trying to do the same thing.. I want a page that shows the second membergroupids 'id' and tells me the title.

Johns Membergroup Ids is: 12, 31

I want it to pick up '31' and tell me the title for '31'. Any Ideas