Usman Qadeer Usman Qadeer - 3 years ago 134
PHP Question

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
}

Answer Source

Just call the function:

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

            );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download