Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:26:08+00:00 2026-06-12T01:26:08+00:00

I have a multiple field like this: <select name=excluded_groups[]> <?php echo $foo->multi_group_select_options($group_ids, $excluded_id); ?>

  • 0

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
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:26:10+00:00Added an answer on June 12, 2026 at 1:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multiselect in html file like this: <select multiple id=mymultiselect name=mymultiselect> <option
I have the following that works like a charm. SELECT d.decisionName, c.firstname, c.lastname, o.name
I have an SQL LIKE: SELECT S.*, (SELECT I.NAME FROM institution I, inst_map IM
I have a lot of rows like this: <input id=car_ids_ name=car_ids[] value=142 type=checkbox> Car
I have a table that contains members names and a field with multiple ID
I have several tables 1 contacts and multiple data. Contacts has an ID field
I have multiple input text fields grouped in divs: <div class=container0> <input class=containerItem0 name=containerItem[0][0]
I have a model hierarchy that looks something like this: Office | +-- Person
I've got this HTML form where I have multiple input elements: text, radio, and
I have three tables, table one (tableA) containing users data like name and email,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.