简体   繁体   English

在多选表单字段中获取选定的值

[英]Get selected values in multiselect form field

I'm updating a PHP form field from a dropdown (single value selected) to a multiselect (multiple values selected). 我正在将PHP表单字段从下拉列表(选择的单个值)更新为多重选择(选择的多个值)。

I have 2 DB tables, one where the complete list of team members reside, and the other where the selected team members reside. 我有2个数据库表,一个表中包含团队成员的完整列表,另一个表中包含选定的团队成员。

On the PHP page, I want to get the values from the full team members table and show the full list as a multiselect form field. 在PHP页面上,我想从完整的团队成员表中获取值,并将完整列表显示为多选表单字段。

Then I want to be able to get the values from the selected team member table and have them show as selected options in the full multiselect list mentioned above. 然后,我希望能够从选定的团队成员表中获取值,并将它们显示为上述完整多选列表中的选定选项。

Any idea on how I would accomplish this? 关于如何实现此目标的任何想法吗?

Here's my code, although right now it just returns the full team member list without the selected values. 这是我的代码,尽管现在它仅返回完整的团队成员列表,但不包含所选值。

    $query = "SELECT walkername FROM Team_Management WHERE active=1 ORDER BY walkername ASC";  
                        $stmt = $mysqli->prepare($query) or die ("Couldn't execute query: ".mysqli_error($mysqli));
                        $stmt->execute();
                        $stmt->bind_result($walkers);

                        echo "<div class='form-group'>";
                        echo "<label class='col-lg-3 control-label' for='Walkers'>Walkers:</label>";
                        echo "<div class='col-lg-5'>";
                        echo "<select multiple class='form-control' name='walkers[]' id='Walkers'>";


                        while ($stmt->fetch()) {
                          echo "<option value='$walkers'>$walkers</option>";
                        }

                        echo "</select>";
                        echo "</div>";
                        echo "</div>";

                        $stmt->close();

** UPDATED ** ** 更新 **

So one thing that I should've added, is that the SELECTED_TEAM_MEMBERS field is a comma separated field. 因此,我应该添加的一件事是SELECTED_TEAM_MEMBERS字段是一个逗号分隔的字段。

TEAM_MANAGEMENT table TEAM_MANAGEMENT表

id || ID || walkername walkername

1 || 1 || John 约翰

2 || 2 || Kate 凯特

SELECTED_TEAM_MEMBER table SELECTED_TEAM_MEMBER表

cid || cid || walkers 步行者

1 | 1 | John,Ray,Kate 约翰·雷,凯特

2 | 2 | Kate,Matt,Joe 凯特,马特,乔

In addition, each group in the walkers field in the SELECTED_TEAM_MEMBER table is tied to a unique client id (cid). 另外,SELECTED_TEAM_MEMBER表中的walkers字段中的每个组都绑定到唯一的客户端ID(cid)。

So how can I identify the selected walkers from the complete list in the TEAM_MANAGEMENT table by unique client id. 因此,如何通过唯一客户端ID从TEAM_MANAGEMENT表的完整列表中识别所选的步行者。

You can get the list of your selected user with a boolean in your SQL request 您可以在SQL请求中使用布尔值获取所选用户的列表

SELECT walkername, 
       CASE WHEN **selected_team_members_name** > '' THEN '1' ELSE '0' END as is_selected 
FROM Team_Management 
   LEFT OUTER JOIN **SELECTED_TEAM_MEMBERS** ON **selected_team_members_name** = walkername
WHERE active=1 
ORDER BY walkername ASC

With selected_team_members_name the name of the column in your table and SELECTED_TEAM_MEMBERS the name of your table 使用selected_team_members_name表中的列的名称,并使用SELECTED_TEAM_MEMBERS表的名称

And now $walker is as array with 2 key: walkername and is_selected 现在$ walker是带有2个键的数组:walkername和is_selected

And after you can try a if to put them the attribute 'selected' when you write your 'option' tag 然后,当您编写“ option”标签时,可以尝试将if属性设置为“ selected”

while ($stmt->fetch())
{
    $selected = "";
    if($walkers['is_selected'] == '1'){ //If your walker is selected
        $selected = "selected";
    }
    echo "<option ".$selected." value=".$walkers['walkername'].">".$walkers['walkername']."</option>";
}

I may not have understand the context but i hope i helped you. 我可能不了解上下文,但希望我能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM