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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化