PHP:使用optgroup进行动态下拉

时间:2011-02-27 08:14:45

标签: php mysql drop-down-menu optgroup

我正在开发一个下拉菜单,该菜单将HTML optgroup用于员工所属的群组名称。这是MySQL查询和输出:

mysql> 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;
+--------+------------+--------+-----------+
| emp_id | empname    | grp_id | groupname |
+--------+------------+--------+-----------+
|     20 | Employee 2 |     13 | Group 1   |
|     19 | Employee 1 |     13 | Group 1   |
|     21 | Employee 3 |     14 | Group 2   |
+--------+------------+--------+-----------+
3 rows in set (0.00 sec)

唯一的问题是,我正在努力弄清楚如何让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 }

getDynGrpList功能截至美国中部时间凌晨3:15(2/27):

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;
}

1 个答案:

答案 0 :(得分:5)

两个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在循环结束后也可用。)

你必须嵌套循环(using alternative syntax for control structures):

<?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'];
}