views:

302

answers:

2

Hi:

I want to add key(type_id) and value(type_description) to select in drupal form API

$result_x->product_types->RPMProductType is array result from Database :- array(4) { [0]=> object(stdClass)#18 (2) { ["type_description"]=> string(10) "Calendered" ["type_id"]=> int(1) } [1]=> object(stdClass)#19 (2) { ["type_description"]=> string(8) "Extruded" ["type_id"]=> int(2) } [2]=> object(stdClass)#20 (2) { ["type_description"]=> string(6) "Molded" ["type_id"]=> int(3) } [3]=> object(stdClass)#21 (2) { ["type_description"]=> string(5) "Other" ["type_id"]=> int(4) } }

foreach ($result_x->product_types->RPMProductType as $data)
{

$form['manufacturer_add_new_sales']['product_type'] = array(
    '#type' => 'select',
    '#title' => t('Product Type'),
    '#options'=>array($data->type_id=>$data->type_description),
    );
}

When do so I am getting only last value i.e Other. How to correctly loop to bind Select to display all the array Key - Values.

Thank you in advance.

+1  A: 

You need to create an array with values and use that.

foreach ($array as $key => $value) {
  $options[$key] = $value;
}

Then you can use $options as your options.

googletorp
A: 

You can also use a function to return an array instead of setting each $key of the $options array.

'#options'=> function_options($param),
....
....
....

// Your Options populating function
function_options($param){
$optionarray = array();
// Populate array with DB values
.....
..... 
return optionarray;
}
Mahesh Marisetti