0

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
  • Here is my solution:- foreach ($resultx->product_types->RPMProductType as $type) { $select_options[$type->type_id] = $type->type_description; } Commented May 6, 2010 at 20:04

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.