0

在一个查询中,我得到了用户表中的所有用户详细信息。

$userDetails =  $dbUser ->getAllUsers();

在第二个查询中,我正在获取所选组中的用户详细信息。

$groupMemberDetails = $dbGroupMembers ->getAllGroupMembers($groupId);

但是,如果用户在选定的组中,我必须将它们显示为选定的剩余用户未选定,并且如果他是所有者,则将其显示为管理员附加到他的名字。

我已经完成了以下代码,但我没有得到正确的。

请纠正我。

foreach ( $userDetails as $user ) {

    $userId = $user['user_id'];
    $userFirstName = $user['first_name'];
    $userName = '';

    $selected = "";
    foreach ( $groupMemberDetails as $groupMemberDetail ) {
        $groupMemberId = $groupMemberDetail['user_id'];
        if($groupMemberId == $userId) {
            $selected = "selected";
        }
        $isAdmin = $groupMemberDetail['is_owner'];
        if($isAdmin) {
            $userName = $userFirstName . "(admin)";
        } else {
            $userName = $userFirstName;
        }

    }
    echo '<option value="'.$userId.'" '.$selected.'>'. $userName . '</option>';
}
4

2 回答 2

0

您需要在每个循环上重置 Selected

  foreach ( $groupMemberDetails as $groupMemberDetail ) {
    $groupMemberId = $groupMemberDetail['user_id'];
    //You need this line
    $selected = '';

    if($groupMemberId == $userId) {
        $selected = "selected";
    }
于 2012-04-09T07:33:32.000 回答
0

在 foreach 之前我已经宣布

 $adminStr = "";

然后在if条件下我这样写

if($groupMemberId == $userId) { 
    $selected = "selected";

    $isAdmin = $groupMemberDetail['is_owner'];
    $adminStr = $isAdmin ? "(Admin)" : "";
}
于 2012-04-26T11:09:49.663 回答