Dynamically load values into a select dropdown menu
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

CCK node reference with views
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
two things
1- if you want to know how to do it manually you may track down the uses of
_taxonomy_term_selectin 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
Not exactly sure where the
Not exactly sure where the data is, but it sounds like you just need the straightforward form stuff (check out the form api).
<?php
$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.
This was exactly what I
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!
www.dueyesterday.net - Documentation for the masses
www.famousfoodfinder.com - Search for restaurants you have seen on TV!
which solution did you go with?
the FAPI and the Taxonomy solutions both sounded good to me. where is the web site?
I just went with the one
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.
www.dueyesterday.net - Documentation for the masses
www.famousfoodfinder.com - Search for restaurants you have seen on TV!
How to retrieve multiple rows of data
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?
The only reason I could think
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:
<?php
$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'),
);
?>
Thanks! it's works. :)
Thanks! it's works. :)