tags:

views:

61

answers:

1

I am dynamically generating a selectlist in Drupal and I want to create an associative array to populate the node ID as the value, and the title of the node as the option.

By default, it makes the value for each option the index of the select list. This is no good because the select list is dynamic, meaning the values won't be in the same order. I used drupal_map_assoc to make the value the same as the option, but I have queries based on the value stored in this field, so if someone updates the value stored, the queries won't match.

<option value="Addison Reserve Country Club  Inc.">Addison Reserve Country Club  Inc.</option>

I want to replace the value with the Node ID also pulled with the query.

 $sql = 'SELECT DISTINCT title, nid FROM {node} WHERE type = \'accounts\' ';
      $result = db_query($sql);
    while ($row = db_fetch_array($result)) {
       $return[] = $row['title'];
       //Trying to do something like  'Addison Reserve Country Club' => '2033' - where 2033 is the nid

     }
      $return = drupal_map_assoc($return);
+1  A: 

I think you just want to do this inside the loop:

$return[$row['nid']] = $row['title'];

Based on your comment, you would also want to do an array_flip() right after the loop, but I think your comment may just have it backwards.

Mike
Mike, you are the man. I had similar syntax in there, but was throwing some errors. I may have been missing the encapsulating bracket on $row['nid'] - nevertheless it works..
cinqoTimo