I've tried this 20 different ways and I can't figure it out. I want to pull some uid's from my DB, and make a dropdown box (form_select). The trick is that I want the usernames as the values and the uid's as the keys. I've already got a function in place to lookup the usernames - 'member_name()'.

Here's the DB query:

$result = db_query("SELECT group_members.uid FROM (group_members) WHERE group_members.gid = '%d'", $node->gid); 

Here's the dropdown:

form_select('dropdown', 'dropdown', NULL, $selections, NULL, 0, FALSE, FALSE);

I want $key to be '$uid' and $value to be 'member_name($uid)'.

I'm using this in the developement of a module. Can anybody help me out? I'm sorry if this is not very clear.

TIA,
Billy

Comments

dman’s picture

$selections = array();
while ($uid = array_pop( db_fetch_array($result) ) ){
  $selections[$uid]=member_name($uid);
}

.. or it may be $selections[member_name($uid)]=$uid;
can't recall.

Is that all you wanted?

.dan.

jeepfreak’s picture

Hmmm... this seems to be working, but it gives a warning:
"warning: array_pop(): The argument should be an array"
Any idea why?
Thanks,
Billy

dman’s picture

You'll get that if it's called on a null result.
Better check if($result) before you get into the while loop.

jeepfreak’s picture

but $result should be NULL, it's got 2 values in it and that is reflected. Sorry, I should just try what you said, but I'm not able to until after work and I'm curious now =)
Thanks a lot!
Billy

EDIT - I just tried your solution and it doesn't change anything. :confused:

jeepfreak’s picture

This would be cake if had PHP5 =(
array_combine()
Billy

EDIT: This function got it to work:

function combine_array($keys, $values)
    {
        if (!is_array($keys)   || !is_array($values) ||
            !$keys || !$values ||
             count($keys) != count($values)) {
            return false;
        }
        reset($keys);
        reset($values);
        $ret = array();
        while (($keyarr = each($keys)) && ($valarr = each($values))) {
           $ret[$keyarr['value']] = $valarr['value'];
        }
        if($keys && $values && empty($ret)) {
            return false;
        }
        return (array)$ret;
    }
jeepfreak’s picture

Now... one last question... how could I add a "none" option? It doesn't even have to be a selectable option, just the default choice?
Thanks,
Billy