2016-03-11 48 views
0

我正在構建選擇下拉列表,當表單提交併通過電子郵件發送給我時,它提交的是選定的下拉列表ID,而不是下拉選項的「值」。獲取數組下拉列表項的值不是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,...)。如果你想要設置的值與文本相同,你可以試試這個

$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')) 
);