0

我想在 drupal 表单 API 中添加 key(type_id) 和 value(type_description) 来选择

$result_x->product_types->RPMProductType 是来自数据库的数组结果:- 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) "其他" ["type_id"]=> int(4) } }

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

$form['manufacturer_add_new_sales']['product_type'] = array(
    '#type' => '选择',
    '#title' => t('产品类型'),
    '#o​​ptions'=>array($data->type_id=>$data->type_description),
    );
}

什么时候这样做我只得到最后一个值,即其他。如何正确循环绑定 Select 以显示所有数组 Key - Values。

先感谢您。

4

2 回答 2

6

您需要创建一个包含值的数组并使用它。

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

然后你可以使用 $options 作为你的选项。

于 2010-05-03T21:40:11.117 回答
0

您还可以使用函数返回一个数组,而不是设置 $options 数组的每个 $key。

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

// Your Options populating function
function_options($param){
$optionarray = array();
// Populate array with DB values
.....
..... 
return optionarray;
}
于 2010-10-22T20:29:12.313 回答