All,

I have searched around through various blogs, Drupal postings and websites to attempt to find my answer, but alas I am stuck. I am trying to do what I feel to be a simple task, but I am not having any luck articulating it well for the search engines to pick up on (or at least I think). Basically what I want to do is provide a user a drop down menu of all possible values a field could be. To add some context to this I will give you an example. Lets say I wanted to query a database to get a list of all possible food selections for a food type field. I would be adding new food choices all the time and as a result would like to have them displayed within the Drupal form. So as my database was updated my drop down full of food items would update as well.

I currently have access to the database information, but given how forms are done within Drupal I am not certain how to iterate over each of my database rows to then add them to the options array within the select form item. Searching online has just pointed me in the direction of using AHAH. Just reading over the examples I have seem on AHAH, it looks far more complex then what I need. Does anyone have any idea how I would solve this problem?

Questions and suggestions are welcome as well.

Thanks,
BD

Comments

ytin’s picture

I haven't tried it yet but maybe you can do this using the CCK node reference field with views as input.

See if it works. HTH

arhak’s picture

1- if you want to know how to do it manually you may track down the uses of _taxonomy_term_select in the taxonomy module (a core module)

2- I strongly recommend you to create a vocabulary for "food" and use taxonomy's select or even autocompletion, then you'll get everything built-in

j_ten_man’s picture

Not exactly sure where the data is, but it sounds like you just need the straightforward form stuff (check out the form api).

$result = db_query('SELECT id, name FROM {table}');
$options = array();
while ($row = db_fetch_object($result)) {
  $options[$row->id] = $row->name;
}

$form['select-box'] = array(
  '#type' => 'select',
  '#options' => $options,
  '#title' => t('My Select Options'),
);

Now, if you need to the select box options to update without a page refresh (using AJAX), then you are going to need some more intense stuff on the backend, similar to what the AHAH documentation talks about.

brandon.dixon’s picture

This was exactly what I needed. I ended up finding and just came back to update this thread with the solution, but thats the same one I came across. Thanks!

irishgringo’s picture

the FAPI and the Taxonomy solutions both sounded good to me. where is the web site?

Doing Native iPHONE, ANDROID, Titanium, node.js and DRUPAL. as a contractor.

brandon.dixon’s picture

I just went with the one listed above. The website I found that above example on was a different Drupal thread. I am not terribly certain which one it was though.

joanne_koh’s picture

I had applied your solution, and it does work for my function, but it only retrieved the last row of data from the database table. I want to retrieve multiple rows of data from the database table for the dropdown menu, so I tried to use a for loop to retrieve them, but it doesnt works. Does anyone have any idea how I would solve this problem?

j_ten_man’s picture

The only reason I could think that it would only give you the last option is because the id is not unique. Try this and see what you get:

$result = db_query('SELECT id, name FROM {table}');
$options = array();
while ($row = db_fetch_object($result)) {
  $options[] = $row->name; //This is the only line that changed from the code above.
}

$form['select-box'] = array(
  '#type' => 'select',
  '#options' => $options,
  '#title' => t('My Select Options'),
);
joanne_koh’s picture

Thanks! it's works. :)

anjjriit’s picture

I my table content is
id | name
1 | one
2 | two
3 | three

how to edit this code to store the correct value

<?php
$result = db_query('SELECT id, name FROM {table}');
$options = array();
while ($row = db_fetch_object($result)) {
  $options[] = $row['name'];    //how to edit this row to store table.id['value'] 
                                //to table2.id['value'].?
}

$form['select-box'] = array(
  '#type' => 'select',
  '#default_value' =>$table2.id  // default value for select-box
  '#options' => $options,    //how to edit this row to store table.id['value']
                             //to table2.id['value'].?
  '#title' => t('My Select Options'),
);
?>

if I select "two" in select box this code was store "1" as default_value, and
if I select "three" in select box this code was store "2" as default_value,

what I want to, is

if I select "one" in select box this code was store "1" as default_value. (not "0")

rfay’s picture

The AHAH and AJAX Examples in the Examples project have dependent dropdown examples.

http://drupal.org/project/examples

-Randy

Pepelu’s picture

I came here for a solution, so once I solved it I decided to post for anyone that needs it. It´s a form with three select lists (dropdowns) and a textarea. The selection on the first dropdown fills the second depending of the selected value, and the second change the third as well. Finally the selection on the third dropdown fills the textarea. The dropdowns and the textarea are filled with queries to the database, so you just have to implements yours, save the results on an array or whatever and pass it to the '#options' to fill the list.

https://github.com/PepeluXX/ajaxFormExample/tree/master