获取数组下拉项的值不是ID

时间:2016-03-11 11:56:35

标签: php drupal

我建立一个选择下拉列表,当表单提交并通过电子邮件发送给我时,它会提交所选的下拉列表ID,而不是下拉选项'价值'代替。

$result = db_query('SELECT title FROM {node} WHERE type =  :type', array(
':type' => 'location',
))->fetchCol();

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

$form['location'] = array(
    //'#prefix' => print_r($result),
    '#type' => 'select',
    '#options' => $items,
    '#attributes' => array('class' => array('search-form'))
);

enter image description here

我尝试使用$ i ++的想法但不太确定是否正确。

//SELECT LOCATION
$result = db_query('SELECT title FROM {node} WHERE type =  :type', array(
':type' => 'location',
))->fetchCol();

$items = array();
$i = 0;
foreach ($result as $key => $value) {
    $items[] = $value;
    $++;
}

$form['location'] = array(
    //'#prefix' => print_r($result),
    '#type' => 'select',
    '#options' => $items[$i],
    '#attributes' => array('class' => array('search-form'))
);

1 个答案:

答案 0 :(得分:1)

我认为你在这里误解了一些东西。你得到的是价值,在你的情况下是一个数字(0,1,......)。如果你想要的是设置值与文本相同,你可以尝试这个

$result = db_query('SELECT title FROM {node} WHERE type =  :type', array(
':type' => 'location',
))->fetchCol();

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

$form['location'] = array(
    //'#prefix' => print_r($result),
    '#type' => 'select',
    '#options' => $items,
    '#attributes' => array('class' => array('search-form'))
);