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