_ajax_example_get_second_dropdown_options

7 ajax_example.module _ajax_example_get_second_dropdown_options($key = '')
8 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

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

Return value

array of options

Related topics

2 calls to _ajax_example_get_second_dropdown_options()

File

ajax_example/ajax_example.module, line 533
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

Populated from db

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:

<?php
*/
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;
  }
}
?>

Login or register to post comments