I have a multiple field like this:
<select name="excluded_groups[]">
<?php echo $foo->multi_group_select_options($group_ids, $excluded_id); ?>
</select>
I manage to get results from the database via this function and put them in the <select>, but I can't manage to keep the selected values selected.
The first parameter should add selected="selected" to the field that were marked before submission and then got submitted, the second parameter prevents a group_id to be displayed (2nd parameter works as it should).
Here is the function...
/**
* 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;
}
The array I am putting as a first parameter is totally valid. It's just a normal array with keys and values, where the values are the group ids.
Tried using foreach inside the while - didn't work, same with outside the while loop.
The $default array looks like this:
Array
(
[0] => 1
[1] => 7
[2] => 2
[3] => 3
)
There are several problems with your code.
First, the function takes $exclude_id as a parameter, but ignores the argument and assigns it with:
$exclude_id = (isset($this->config['default_group'])) ? $this->config['default_group'] : 5;
Second, you say that this should be a multi-select, but your <SELECT> tag doesn't have the MULTIPLE attribute.
Third, you're comparing $row['group_id'] to $group_id. This only allows for a single default, not multiple, and you're not even setting the variable (the code that sets it is commented out, but it would only set it to the last element of the $default array. DCoder's comment seems like the correct solution to this.
You're not setting $group_id. The following code will work with an array of default values as well as a single value:
if(is_array($default)) {
$selected = in_array($row['group_id'], $default);
} else {
$selected = !strcasecmp($row['group_id'], $default);
}
$selected = $selected ? ' selected="selected"' : '';
In addition to that, please look into the notes @Barmar wrote in his answer, and use htmlspecialchars to escape any dynamic text such as $s_group_name.
This comment is not related to your specific problem, but you should still consider it.
Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With