【问题标题】:Multiple select field - use selected="selected" multiple times多选字段 - 多次使用 selected="selected"
【发布时间】:2012-09-11 10:14:41
【问题描述】:

我有一个这样的多个字段:

<select name="excluded_groups[]">
    <?php echo $foo->multi_group_select_options($group_ids, $excluded_id); ?>
</select>

我设法通过这个函数从数据库中获取结果并将它们放入&lt;select&gt;,但我无法保持选中的值。

第一个参数应该将selected="selected"添加到提交前标记然后提交的字段,第二个参数防止显示group_id(第二个参数正常工作)。

这里是函数...

/**
* group_options
* Get group names in the dropdown list
*/
public function multi_group_select_options($default = false, $exclude_id = '')
{
    global $user;

    $exclude_id = (isset($this->config['default_group'])) ? $this->config['default_group'] : 5;

    $sql_where = ($user->data['user_type'] == USER_FOUNDER) ? '' : 'WHERE group_founder_manage = 0';
    $sql_where_and = (!empty($sql_where)) ? ", AND group_id <> $exclude_id" : "WHERE group_id <> $exclude_id";
    $sql = 'SELECT group_id, group_name, group_type
            FROM ' . GROUPS_TABLE . "
            $sql_where
            $sql_where_and
            ORDER BY group_name";
    $result = mysql_query($sql);

    $s_group_options = '';
    while ($row = mysql_fetch_assoc($result))
    {
        /*if (is_array($default))
        {
            break;
            $group_id = '';
            foreach ($default as $key => $group_id)
            {
                $group_id = $group_id;  
            }
        }

        print_r($default);*/

        $selected = ($row['group_id'] == $group_id) ? ' selected="selected"' : '';
        $s_group_name = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
        $s_group_options .= '<option'  . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . $s_group_name . '</option>';
    }
    $db->sql_freeresult($result);

    return $s_group_options;
}

我作为第一个参数放置的数组是完全有效的。它只是一个带有键和值的普通数组,其中的值是组 ID。

尝试在 while 内使用 foreach - 不起作用,与 while 循环外相同。

$default 数组如下所示:

Array
(
    [0] => 1
    [1] => 7
    [2] => 2
    [3] => 3
)

【问题讨论】:

  • 你没有设置$group_id。请改用$selected = (is_array($default) &amp;&amp; in_array($row['group_id'], $default)) ? ' selected="selected"' : '';
  • 您的解决方案奏效了!谢谢。您能否发表您的评论作为答案,以便我接受?

标签: php arrays select default


【解决方案1】:

您的代码有几个问题。

首先,该函数将$exclude_id 作为参数,但忽略该参数并将其赋值为:

$exclude_id = (isset($this->config['default_group'])) ? $this->config['default_group'] : 5;

第二,你说这应该是多选,但是你的&lt;SELECT&gt;标签没有MULTIPLE属性。

第三,您将$row['group_id']$group_id 进行比较。这只允许一个默认值,而不是多个,你甚至没有设置变量(设置它的代码被注释掉,但它只会将它设置为 $default 数组的最后一个元素。DCoder 的评论似乎喜欢这个问题的正确解决方案。

【讨论】:

    【解决方案2】:

    你没有设置$group_id。以下代码将使用默认值数组以及单个值:

    if(is_array($default)) {
      $selected = in_array($row['group_id'], $default);
    } else {
      $selected = !strcasecmp($row['group_id'], $default);
    }
    $selected = $selected ? ' selected="selected"' : '';
    

    除此之外,请查看@Barmar 在his answer 中写的注释,并使用htmlspecialchars 转义任何动态文本,例如$s_group_name


    此评论与您的​​具体问题无关,但您仍应考虑。

    请停止使用古老的 mysql_* 函数编写新代码。它们不再被维护,社区已经开始deprecation process。相反,您应该了解准备好的语句并使用PDOMySQLi。如果你想学习,here is a quite good PDO-related tutorial

    【讨论】:

    • 感谢您对 mysql_ 函数的评论。我会注意你说的话。
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2015-05-31
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2013-10-10
    • 2019-04-09
    相关资源
    最近更新 更多