从mysql回忆下拉菜单需要独特的选择

时间:2014-12-28 00:35:48

标签: php mysql drop-down-menu

我正在学习PHP和MySQL,并且这样做我正在尝试做一个小型数据库驱动的网站。我正在修改数据库驱动的下拉表单列表,到目前为止可以在数据输入表单上显示它们,可以捕获结果并将其插入到数据库中,现在我正在尝试调用所选项目,将其显示在更新/编辑表单上。到目前为止,使用下面的代码我可以在下拉列表中将数据调用到更新/编辑表单中,但是我想将可选项缩小到仅限于唯一角色。

我的代码是:

<?php

include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";

$role_query = "SELECT fer_users.id fer_roles_id, fer_roles.description\n"
    . "FROM fer_users\n"
    . "JOIN fer_roles\n"
    . "WHERE fer_users.fer_roles_id=fer_roles.id";
$role_result = mysqli_query($connection, $role_query);
?>

<select name="update_role">
    <?php 
    while ($role_row = mysqli_fetch_assoc($role_result)) {  
    $role_id=$role_row["fer_roles_id"];
    $role_description=$role_row["description"];
    $selected = ($role_row['fer_roles_id'] == $id);
    echo "<option value=\"$role_id\" ".($selected ? " selected=\"selected\"":"").">$role_description</option>";
    }
    ?>
</select>  

SQL查询的结果如下:

id |     role
-----------------
8  | Administrator
16 | Administrator
9  |     User
14 |     User
15 |     User

我的下拉列表呈现如下: enter image description here

如上所述,我需要图片中的列表才能显示管理员和用户。

4 个答案:

答案 0 :(得分:1)

只需运行两个查询。

<?php

include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";

$role_query = "SELECT fer_roles.id, fer_roles.description\n"
. "FROM fer_roles\n"
. "WHERE 1";
$role_result = mysqli_query($connection, $role_query);
$role_query2 = "SELECT fer_users.id, fer_users.fer_roles_id \n"
. "FROM fer_users\n"
. "WHERE id = '".mysql_real_escape_string($_GET['id'])."'"; //this assumes you're passing the user as a GET variable
$role_result2 = mysqli_query($connection, $role_query2);
$role_row2 = mysqli_fetch_assoc($role_result2);
?>

<select name="update_role">
<?php 
while ($role_row = mysqli_fetch_assoc($role_result)) {  ?>
    <option value="<?php echo $role_row['id'];?>" <?php if($role_row['id'] == $role_row2['fer_roles_id']){ echo 'selected="selected"';}?>><?php echo $role_row['description'];?></option>";
}
?>
</select>  

可能不是最优雅的答案,但它应该完成工作:)

编辑:以上是您尝试更新某个特定用户的情况。如果您想要所有用户及其角色的列表,可以这样做:

<?php

include $_SERVER['DOCUMENT_ROOT']."/connections/connectdb.php";

$role_query = "SELECT fer_roles.id, fer_roles.description\n"
. "FROM fer_roles\n"
. "WHERE 1";
$role_result = mysqli_query($connection, $role_query);
$role_query2 = "SELECT fer_users.id, fer_users.fer_roles_id \n"
. "FROM fer_users\n"
. "WHERE 1"; //this assumes you're looking up a specific user
$role_result2 = mysqli_query($connection, $role_query2);
while($role_row2 = mysqli_fetch_assoc($role_result2)){
?>

    <select name="update_role">
    <?php 
    while ($role_row = mysqli_fetch_assoc($role_result)) {  ?>
        <option value="<?php echo $role_row['id'];?>" <?php if($role_row['id'] == $role_row2['fer_roles_id']){ echo 'selected="selected"';}?>><?php echo $role_row['description'];?></option>";
    }
?>
</select>  
<?php
} ?>

答案 1 :(得分:0)

添加GROUP BY fer_roles.description

到查询结尾。这会根据角色描述将结果分组。

要包含每个角色的所有ID,也请更改

SELECT fer_users.id fer_roles_id, fer_roles.description

SELECT GROUP_CONCAT(fer_users.id fer_roles_id SEPARATOR ','), fer_roles.description

这将导致id字段返回每个ID的逗号分隔列表。

GROUP_CONCAT()

上的文档

答案 2 :(得分:0)

<?php 
    $roles = array();
    while ($role_row = mysqli_fetch_assoc($role_result)) {  
       $roles[$role_row["fer_roles_id"]]=$role_row["description"];
    ?>
<select name="update_role">
    <?php 
    foreach($roles as $role_id => $role){
       $selected = ($role_id == $id);
       echo '<option value="'.$role_id.'" '.($selected ? ' selected ' : '').'>'.$role.'</option>';
    }
    ?>
</select>

答案 3 :(得分:0)

您是否希望返回的ID是用户ID或角色ID?

如果是前者,那么你最终会得到重复项,因为可能有多个用户拥有这些角色。

如果是后者,那么你不想修改select只能查看Roles表吗?