一尘不染

PHP:使用optgroup动态下拉列表

mysql

我正在开发一个下拉菜单,该菜单使用HTML optgroups作为员工所属的组名。这是MySQL查询和输出:

mysql> SELECT employee.emp_id,employees.empname,employees.grp_id,groups.groupname FROM雇员离开的雇员加入员工组。grp_id= groups.grp_id,按组名asc排序;
+ -------- + ------------ + -------- + ----------- +
| emp_id | empname | grp_id | 组名|
+ -------- + ------------ + -------- + ----------- +
| 20 | 员工2 | 13 | 第1组|
| 19 | 员工1 | 13 | 第1组|
| 21 | 员工3 | 14 | 第2组|
+ -------- + ------------ + -------- + ----------- +
设置3行(0.00秒)

唯一的问题是,我很难确定如何使optgroup正常工作。我尝试了无数次,这真的开始让我感到沮丧。 以下是我想要的输出示例(示例):

<select name="dropdownmenu">
    <optgroup label="Group 1">
        <option name="20">Employee 2</option>
        <option name="19">Employee 1</option>
    </optgroup>
    <optgroup label="Group 2">
        <option name="21">Employee 3</option>
    </optgroup>
</select>

基本上,optgroup必须是“ groupname”,选项“ name”应该是“ emp_id”,而动作“ option”(下拉项)是“
empname”。

我希望这是可以做的,但是真的不确定。这是我所拥有的功能,但无法正常运行:

function getDynGrpList() {
    global $db;

    // $query = "SELECT * FROM employees ORDER BY grp_id desc;";
    $query = "SELECT employees.emp_id,employees.empname,employees.grp_id,groups.groupname FROM employees left join groups on employees.grp_id = groups.grp_id order by groupname asc;";
    $employees = $db->GetAll($query);
    $groups = array();

    while ($qa = $employees->GetRows()) {
        $groups[$qa['groupname']][$qa['grp_id']] = $qa['empname'];
    }
    foreach ($groups as $label => $opt) { ?>
        <optgroup label="<?php echo $label; ?>">
<?php   }
        foreach ($groups[$label] as $id => $name) { ?>
            <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
        </optgroup>
<?php }

从凌晨3:15 CST(2/27)开始的getDynGrpList函数:

function getDynGrpList() {
    global $db;

    // $query = "SELECT * FROM employees ORDER BY grp_id desc;";
    $query = "SELECT employees.emp_id,employees.empname,employees.grp_id,groups.groupname FROM employees left join groups on employees.grp_id = groups.grp_id order by groupname asc;";
    $employees = $db->GetAll($query);
    $groups = array();
    while ($qa = $employees->GetRows()) {
        $groups[$qa['groupname']][$qa['emp_id']] = $qa['empname'];
    }
    var_export($groups);
    foreach($groups as $label => $opt): ?>
        <optgroup label="<?php echo $label; ?>">
    <?php foreach ($opt as $id => $name): ?>
        <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
    <?php endforeach; ?>
    </optgroup>
<?php endforeach;
}

最终解决方案(在Felix Kling的帮助下)

function getDynGrpList() {
    global $db;

    $query = "SELECT employees.emp_id,employees.empname,employees.grp_id,groups.groupname FROM employees left join groups on employees.grp_id = groups.grp_id order by groupname asc;";
    $employees = $db->GetAll($query);
    $groups = array();
    foreach ($employees as $employee) {
        $groups[$employee['groupname']][$employee['emp_id']] = $employee['empname'];
    }
    foreach($groups as $label => $opt): ?>
        <optgroup label="<?php echo $label; ?>">
    <?php foreach ($opt as $id => $name): ?>
        <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
    <?php endforeach; ?>
    </optgroup>
<?php endforeach;
}

阅读 452

收藏
2020-05-17

共1个答案

一尘不染

这两个for循环未嵌套在您的代码中:

foreach ($groups as $label => $opt) { ?>
    <optgroup label="<?php echo $label; ?>">
<?php   } <-- wrong here
    foreach ($groups[$label] as $id => $name) { ?>
        <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>

结果是首先创建了所有的opt组,然后添加了最后一个组的雇员(因为$label并且$opt在循环完成后也可以使用)。

您必须嵌套循环( 对控制结构使用替代语法 ):

<?php foreach($groups as $label => $opt): ?>
    <optgroup label="<?php echo $label; ?>">
    <?php foreach ($opt as $id => $name): ?>
        <option value="<?php echo $id; ?>"><?php echo $name; ?></option>
    <?php endforeach; ?>
    </optgroup>
<?php endforeach; ?>

此外,我认为您在创建数组时必须使用emp_id,而不是grp_id

while ($qa = $employees->GetRows()) {
    $groups[$qa['groupname']][$qa['emp_id']] = $qa['empname'];
}
2020-05-17