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);
CommentFileSizeAuthor
#2 extra_fields.png2.09 KBycimlynn

Comments

nevets’s picture

$newarray[$i] = $results->name; should probably be $newarray[$results->uid] = $results->name;

ycimlynn’s picture

StatusFileSize
new2.09 KB

Oh 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.

nevets’s picture

Not 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.

panthar’s picture

Nevets/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.

nevets’s picture

From the image ycimlynn provided the 0 is from the code that handles the returned array.

megachriz’s picture

Status: Active » Fixed

@ycimlynn
In the last line of your code you are returning an array in an array, which will result in something like this:

Array
(
    [0] => Array
        (
            [0] => Please select
            [1] => Bob T
            [2] => Sam G
        )

)

Return a one dimensional array and the result will become something like this:

Array
(
    [0] => Please select
    [1] => Bob T
    [2] => Sam G
)

What the last code line should be:

return $newarray;

One other note: if this field is a required field, use an empty string for the 'Please select' instead of '0'.

Full code example:

$newarray = array();
$newarray[''] = 'Please select';
$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[$results->uid] = $results->name;
  }
}

return $newarray;
ycimlynn’s picture

Status: Fixed » Closed (cannot reproduce)

Thanks for your help. That got it!
:)

megachriz’s picture

Status: Closed (cannot reproduce) » Closed (fixed)

I had reproduced your issue ;)