Hello! i am trying to create a select list based upon the users of a selected role. The code is listed below.
A couple of issues...
First of all, the array returns a "0" as the first line in the select list, that you are unable to click on/choose, and it is not at all related to the users in my list.
Second, it does return a listing, but when you choose the correct item/user name, it does not save it.
Any thoughts on this issues are appreciated. Guidance is always welcome... :)
$newarray = array();
$newarray[0] = 'Please select';
$i = 1;
$qry = db_query("SELECT u.name, u.uid FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid = 6 ORDER BY name");
while ($results = db_fetch_object($qry))
{
if ($results->uid != 0) {
$newarray[$i] = $results->name;
$i++;
}
}
return array($newarray);
Comments
Comment #1
nevets commented$newarray[$i] = $results->name;should probably be$newarray[$results->uid] = $results->name;Comment #2
ycimlynn commentedOh my goodness!! You are totally correct on that issue. I can't believe I didn't see that. Thank you.
Any clues on the "0" in the top line of the select box?
Screenshot is attached.
Comment #3
nevets commentedNot knowing the module, it appears it is building a select group (notice how everything else in indented). So the question is how is the returned array being used.
Comment #4
panthar commentedNevets/ycimlynn ,
To answer your question, my best guess would be that 0 is the "anonymous" user role, and the uid is also 0?
If that's not the problem, could you dvm() or print_r() the array contents right before the return of the function, and post it here?
Thanks.
Comment #5
nevets commentedFrom the image ycimlynn provided the 0 is from the code that handles the returned array.
Comment #6
megachriz@ycimlynn
In the last line of your code you are returning an array in an array, which will result in something like this:
Return a one dimensional array and the result will become something like this:
What the last code line should be:
One other note: if this field is a required field, use an empty string for the 'Please select' instead of '0'.
Full code example:
Comment #7
ycimlynn commentedThanks for your help. That got it!
:)
Comment #8
megachrizI had reproduced your issue ;)