Menu

#169 Indexes for Dropdown boxes

open
nobody
None
5
2012-07-20
2011-06-13
Anonymous
No

I have a custom field which needs to store an index, but display a value. I thought it would be easy to implement like so:

$select_options['entry.facilitator'] = array(
'username1' => 'Full Name 1',
'username2' => 'Full Name 2',
'username3' => 'Full Name 3',
'username4' => 'Full Name 4',
'username5' => 'Full Name 5');

The reason for this is as I authenticate with LDAP, I want populate this list with a list of facilitators which have to be looked up later by username.

A quick change to the generate_select function in "functions.inc" seems to work for me. Note, the following code isn't completely fool-proof. It assumes that any keyed list wouldn't have an index of 0.

function generate_select($label_text, $name, $value, $options,
$mandatory = FALSE, $disabled=FALSE)
{
// generate the HTML
$html = "<label for="\\"$name\\"">$label_text</label>\n";
$html .= "<select id="\\"$name\\"" name="\\"$name\\"";" $html="" .="($disabled)" ?="" "="" disabled="\\"disabled\\""" :="" '';="" $html="" .="&gt;\n" ;<br=""> if ($mandatory)
{
$options = array_merge(array(""),$options);
}
while ($option = current($options))
{
var_dump(key($options));
$html .= "<option";

if (!array_key_exists(0, $options)) {
    $html .= (isset($value) && ($value == key($options))) ? " selected=\"selected\"" : ''; 
    $html .= " value=\"".key($options)."\">";
    $html .= htmlspecialchars($option)."</option>\n";
} else {
    $html .= (isset($value) && ($value == $option)) ? " selected=\"selected\"" : '';    
    $html .= ">".htmlspecialchars($option)."</option>\n";
}
next($options);

}
$html .= "</select>\n";
// and a hidden input if the select box is disabled
if ($disabled)
{
$html .= "<input type="\\"hidden\\"" name="\\"$name\\"" value="\\""." htmlspecialchars($value)."\\"="">\n";
}

echo $html;
}

Discussion

  • Campbell Morrison

    While this works for the edit_entry form, you'd also have to change MRBS so that the value rather than the key is shown in view_entry.php and in email notifications, and you'd have to change Search and Report so that they search for the value rather than the key - so this would be quite a bit of change to implement in standard MRBS. Obviously not a huge amount, but before doing so I'd be interested whether other people have the same requirement.

    Campbell

    PS Another way of testing for an associative array is to use the function described by magentix at gmail dot com at http://php.net/manual/function.is-array.php

    function is_assoc ($arr)
    {
        return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
    }
    
     
  • Anonymous

    Anonymous - 2011-06-16

    Campbell,

    Thanks for your input, and for taking the time to look at my suggestion. I've modified my code accordingly with your P.S. suggestion, and also view_entry.php.

    If there is interest, I would love to share more of my thoughts/code on this subject. Essentially, we have to assign a facilitator to hand many of our scheduled events. I'm not sure if anyone else needs to do this, but if so perhaps it would be a good feature addition.

    P.S. I'm upgrading from 1.4.3. to 1.4.6. I'm loving all of the new features! Great work!

    Sincerely,

    -Shane

     
  • Campbell Morrison

    I've implemented this now in the trunk - Rev 1837

    Campbell