Search code examples
phparraysdrupal-7

Array result of a function as options in Drupal select option


I want to use return array of a function as options in select list using form fields in drupal.

Here is code of select box field.

$form ['user_details']['course'] = array(
          '#type' => 'select',
          '#title' => 'course:',
          '#page arguments' => array('get_course'),
          '#options' => $course,
);

Here is the function from which i am getting array.

function get_course() {
    $course = array();
    $query = db_select('mdl_course', '');
    $query->fields('fullname', array(''));
    $query->condition('category', 8);
    $result = $query->execute();
    $course = $result;
    return $course
}

Solution

  • Just call the function:

    $form ['user_details']['course'] = array(
             '#type' => 'select',
              '#title' => 'course:',
             '#page arguments' => array('get_course'),
              '#options' =>  get_course(),
    
                );