开发者

Creating an associative array using a while loop for select list?

开发者 https://www.devze.com 2022-12-15 20:37 出处:网络
I am dynamically generating a selectlist in Drupal and I want to create an associative a开发者_JAVA百科rray to populate the node ID as the value, and the title of the node as the option.

I am dynamically generating a selectlist in Drupal and I want to create an associative a开发者_JAVA百科rray 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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消