I have been recently trying to use drupal as an itranet based CMS for my company and i started out by using CCK to create my custom nodes. I noticed that CCK, although powerfull is in places, a little limted. I decided to write my own modules to get the structure i wanted. What i an looking for is when i create my form for my module. I want to have a pull down menu that is generated from another table in my database. So instead of a simple pull down menu like this:

$form['basic_attributes']['personal_title'] = array(
'#type' => 'select',
'#title' => t('Title'),
'#default_value' => $node->personal_title,
'#options' => array(
'Mr' => 'Mr',
'Mrs' => 'Mrs',
'Miss' => 'Miss',
'Ms' => 'Ms'
),
'#description' => t('Please choose an option.'),
);

I want to create something like this: (this is NOT the actual syntax, just an explanation)

// Load the data to create the menu

$record = query(SELECT * FROM pull_down_menu_table);

$form['basic_attributes']['personal_title'] = array(
'#type' => 'select',
'#title' => t('Title'),
'#default_value' => $node->personal_title,

// Generate the pull down options

foreach $record blah blah blah
),
'#description' => t('Please choose an option.'),
);

No, im new to Drupal module development but quite confident with php. I just want to know the basic rules of creating dynamic pull downs menus based on other tables?

Any pointers in where to start?

Best regards,

Harris

Comments

lenkkivihko’s picture

I am looking for solution on this...

Create a content (=e.g., shoe review). This is CCK content type1.
Then have a pull-down menu in CCK content type2 that has all CCK content type1's in the pull down.
A dream would be that the default value of CCK content type1 would be the one selected last time / of not available the one created by the same user. (=e.g, I have used these shoes)

Can someting like this be done with CCK or do I need a custom module?
If a custom module is required, please provide pointers.

Many thanks in advance!
==================
EDIT - 2 good pages:
Populate drop-down lists:
Perhaps this could help us: http://drupal.org/node/48843

This provides tutorial to create custom content module:
http://drupal.org/node/71954

Harjoituspäiväkirja - www.lenkkivihko.fi

dwees’s picture

I do this in a module, and it's relatively straight forward for a PHP programmer. Just create the $options array from the information you want in the query, and THEN create a form structure like so:

$form['basic_attributes']['personal_title'] = array(
'#type' => 'select',
'#title' => t('Title'),
'#default_value' => $node->personal_title,
'#options' => $options,
'#description' => t('Please choose an option.'),
);
hazamonzo’s picture

Looks good, I assume i use db_query function to get the information from my table in the drupal database? and if i use the db_query function, in what format does the function return the results? an array of records?

Best regards,

Harris

dwees’s picture

So supposed you've done something like:

$result = db_query("SELECT title FROM {node} n WHERE n.uid = %d", $uid);

You now have an object which contains the results of the query. Now you use something like this to convert your options into an array like so:


while ($row = db_fetch_array($result)) {
      $options[] = $row['title'];
}

to construct the options array.

Hope this helps.