在一个表单中使用form_dropdown,并带有以下选项:
<select name="depto">
<option value="">[todos los departamentos]</option>
<option value="0001">First</option>
<option value="01">Second</option>
</select>
当字段的任何值为空时,即“0001”或“01”,在两种情况下都会生成form_dropdown
<select name="depto">
<option value="">[todos los departamentos]</option>
<option selected="selected" value="0001">First</option>
<option selected="selected" value="01">Second</option>
</select>
标记为选择了2个选项。
查看表单助手,in_array()函数用于检查值。
我更改了form_helper以使用完全匹配来调用in_array(),这是第三个参数为'true'并开始按照指定的方式工作。
你看到这个改变有什么问题吗?
较小的更改标有/// HERE THE ,true ADDED
以下是该函数的代码:
/**
* Drop-down Menu
*
* @access public
* @param string
* @param array
* @param string
* @param string
* @return string
*/
if ( ! function_exists('form_dropdown'))
{
function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
{
if ( ! is_array($selected))
{
$selected = array($selected);
}
// If no selected state was submitted we will attempt to set it automatically
if (count($selected) === 0)
{
// If the form name appears in the $_POST array we have a winner!
if (isset($_POST[$name]))
{
$selected = array($_POST[$name]);
}
}
if ($extra != '') $extra = ' '.$extra;
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
foreach ($options as $key => $val)
{
$key = (string) $key;
if (is_array($val))
{
$form .= '<optgroup label="'.$key.'">'."\n";
foreach ($val as $optgroup_key => $optgroup_val)
{
$sel = (in_array($optgroup_key, $selected, true)) ? ' selected="selected"' : ''; /// HERE THE ,true added
$form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
}
$form .= '</optgroup>'."\n";
}
else
{
$sel = (in_array($key, $selected, true)) ? ' selected="selected"' : ''; /// HERE THE ,true ADDED
$form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
}
}
$form .= '</select>';
return $form;
}
}
答案 0 :(得分:0)
尝试将您的值转换为字符串,如下面的示例所示,看看是否有效。
$deptolist = array( (string)'0001' => 'First', (string)'01' => 'Second' );