doubao7287 2012-02-27 03:42
浏览 8
已采纳

Drupal 7:选择下拉列表中不需要的字符

What I need is a dropdown that default is blank. It has a '-create new client-' field and then a list of clients. Here is the code for the menu field:

$form['client']['existing'] = array(
  '#title' => t('Client'),
  '#type' => 'select',
  '#options' => array('add' => t('add new client'),$fullName),
  '#empty_option' => '',
  '#description' => t('Select an existing Client or choose -create new client-'),
);

Here's the function I use to populate $fullName:

function myForm_get_names()
{
  $query = db_select('client', 'c');
  $query->fields('c', array('cid','fname','lname'));
  $fullName = array();
  $result = $query->execute();

  foreach ($result as $r)
  {
    $fullName[$r->cid] = $r->fname . " " . $r->lname;
  }
  return $fullName;
}

Here's the print out of $fullName

array (
  1 => 'tim arthur',
  2 => 'val brant',
)

Everything works great. The only problem is it inserts a number after -add new client-. so it's looks like:

Client:
blank
-add new client-
0
  tim arthur
  val brant

I printed out the values of my option field:

'#options' => array(
  'add' => 'add new client',
  0 => array(
    1 => 'tim arthur',
    2 => 'val brant',
  ),
),

the number increases if I give -add new client- a numeric value. For example, if I do:

'#options' => array ('5' => '-add new client-', $fullNames),

then the 0 turns to a 6.

Any ideas on how I can get my array to not be nested. I also tried this, but got an error:

'#options' => array (
  'add' => '-add new client-', 
  array_keys($fullNames) => array_values($fullNames)), 
  • 写回答

2条回答 默认 最新

  • dsw7547 2012-03-02 19:37
    关注

    Turns out the solution was quite simple. So instead of

    '#options' => array('add' => t('add new client'),$fullName),
    

    just do

    '#options' => $fullName,
    

    and then in my myForm_get_names() function instead of

    $fullName = array();
    

    do

    $fullName = array('add' => '-add new client-');
    

    didn't have to change anything else. h0tw1r3's array_merge idea was crafty tho.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源