次のような複数フィールドがあります。
<select name="excluded_groups[]">
<?php echo $foo->multi_group_select_options($group_ids, $excluded_id); ?>
</select>
この関数を介してデータベースから結果を取得し、<select>
.
selected="selected"
最初のパラメーターは、送信前にマークされてから送信されたフィールドに追加する必要があります。2 番目のパラメーターは、group_id が表示されないようにします (2 番目のパラメーターは正常に機能します)。
ここに関数があります...
/**
* 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
)