I am trying to return dynamic select options for a webform component. I have it working to return node titles, but I need to instead return values from a manually created table in the db that is not drupal related. How would I alter this function to achieve that?

function _MY_MODULE_get_rate_options() {
    $nodes = array();
    $select = db_query("MY DB QUERY HERE");
    foreach ($select as $node) {
        $nodes[$node->nid] = $node->title;
    return $nodes; 

If you're creating a select list then you only need two fields from the database, so you can use the excellent fetchAllKeyed() method of the database query. This will let you get your results in a single line of code:

function _MY_MODULE_get_rate_options() {
  return db_query('SELECT id, name FROM {my_table}')->fetchAllKeyed();

The result is an array keyed by the first field (id) with values from the second field (name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.