PHP:动态下拉与optgroup [英] PHP: Dynamic Drop down with optgroup

查看:95
本文介绍了PHP:动态下拉与optgroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a drop down menu that uses HTML optgroups for group names that employees are a part of. Here is the MySQL query and output:


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正常工作。我尝试了无数次,真的开始挫败我。 以下几乎是我想要的输出(例如):

The only issue is, I'm having the hardest time figuring out how to get the optgroup to work correctly. I've tried countless times, and it's really starting to frustrate me. The following is pretty much I want the output to be (example):

<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,动作选项(下拉项)是empname。

Basically, the optgroup needs to be the "groupname", the option "name" should be the "emp_id", and the action "option" (drop down item) is the "empname".

我希望这是可以做到的,但真的不确定。这是我有的功能,但它并不能很好地工作:

I hope this is something that can be done, but really not sure. Here's the function I have, but it doesn't exactly work well:

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


推荐答案

嵌套在代码中:

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

结果是首先创建所有选择组,然后添加最后一个组的员工(因为在循环完成后,$ code> $ label 和 $ opt 也可用。

The result is that first all opt groups are created and then the employees for the last group are added (because $label and $opt are also available after the loop finished).

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

You have to nest the loops (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 创建数组时:

Furthermore, I think you have to use the emp_id, not the grp_id when creating the array:

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

这篇关于PHP:动态下拉与optgroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆