如何获得第一个下拉值作为无

马杜·奈尔(Madhu Nair)

我从下面的代码中获得了所需的选项,但是我需要添加一个空选项作为返回数组的第一个值,'' => '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();
}

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

系统调用

您可以在输入数据之前添加条目:

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"
  ...
}

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

array(1) {
  [""]=>
  string(4) "none"
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章