如何获得第一个下拉值为none

时间:2018-03-16 13:18:12

标签: php drupal drupal-database

我从以下代码中获得了所需的选项,但是我需要添加一个空选项作为返回数组的第一个值'' => 'none',,然后添加其余值。

function dropdown() {
  return db_select('node', 'n')
    ->condition('n.type', 'abc')
    ->condition('n.status', 1)
    ->fields('n', array('nid', 'title'))
    ->orderBy('n.title', 'ASC')
    ->execute()
    ->fetchAllKeyed();
}

但是,这只提供了数据库中的值。

1 个答案:

答案 0 :(得分:5)

您可以在之前添加条目以返回数据:

?

可能的输出:

function dropdown() {
  $data = db_select('node', 'n')
    ->condition('n.type', 'abc')
    ->condition('n.status', 1)
    ->fields('n', array('nid', 'title'))
    ->orderBy('n.title', 'ASC')
    ->execute()
    ->fetchAllKeyed();
  return ['' => 'none'] + $data ;
}

如果您的条件没有可用的节点,它将返回:

array(463) {
  ['']=>
  string(4) "none"
  [367]=>
  string(7) "Title 1"
  [63]=>
  string(7) "Title 2"
  ...
}