7.x-1.x ajax_example.module _ajax_example_get_second_dropdown_options($key = '')

Helper function to populate the second dropdown.

This would normally be pulling data from the database.

Parameters

string $key: This will determine which set of options is returned.

Return value

array Dropdown options

Related topics

2 calls to _ajax_example_get_second_dropdown_options()
ajax_example_dependent_dropdown in ajax_example/ajax_example.module
AJAX-based dropdown example form.
ajax_example_dependent_dropdown_degrades in ajax_example/ajax_example_graceful_degradation.inc
Dropdown form based on previous choices.

File

ajax_example/ajax_example.module, line 649
AJAX Examples module file with basic examples.

Code

function _ajax_example_get_second_dropdown_options($key = '') {
  $options = array(
    t('String') => drupal_map_assoc(array(
      t('Violin'),
      t('Viola'),
      t('Cello'),
      t('Double Bass'),
    )),
    t('Woodwind') => drupal_map_assoc(array(
      t('Flute'),
      t('Clarinet'),
      t('Oboe'),
      t('Bassoon'),
    )),
    t('Brass') => drupal_map_assoc(array(
      t('Trumpet'),
      t('Trombone'),
      t('French Horn'),
      t('Euphonium'),
    )),
    t('Percussion') => drupal_map_assoc(array(
      t('Bass Drum'),
      t('Timpani'),
      t('Snare Drum'),
      t('Tambourine'),
    )),
  );
  if (isset($options[$key])) {
    return $options[$key];
  }
  else {
    return array();
  }
}

Comments

nicodv’s picture

Hello, I've been 5 days trying to make work my dependent dropdowns. The code I used is the same one of the examples module, but I think my problem is populating (and linking) the dropdowns.
Can anyone help me? Is this correct? 1000 thanks in advance:

 */
function _myform_get_sector_options() {
  $sectors = array();
  $sector_result = db_query("SELECT sector_id, sector_name FROM {sectors}");
  foreach ($sector_result as $record) {
	$sectors[$record->sector_id] = $record->sector_name;
  }
  return $sectors;
}

/**
 * Helper function to populate the second dropdown. This was accomplished with drupal_map_assoc, but
 * i'll try with a db query...
 *
 * @param $key
 *   This will determine which set of options is returned.
 *
 * @return array of options
 */
function _myform_get_activity_options($key = '') {
$options = array();
  $sector_result = db_query("SELECT sector_id FROM {sectors}");
  foreach ($sector_result as $record) {
    $sector_id = $record->sector_id;
  
    //$sector_id = $key;
  
	$activity = array();
    $activity_result = db_query("SELECT activity_name, activity_id FROM {activity} WHERE sector_id = :sector_id",
	array(':sector_id' => $sector_id));
	foreach ($activity_result as $record_2) {
	  $activity[$record_2->activity_id] = $record_2->activity_name;
	}
  }
  $options[$key] = $activity;
  
  if (isset($options[$key])) {
    return $options[$key];
  }
  else {
      $options1 = array('0' => '-- Select Activity --');
    return $options1;
  }
}