Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

mysql - PHP: Dynamic Drop down with optgroup

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)

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>

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 function as of 3:15AM 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;
}

Final Solution (with the help of 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The two for loops are not nested in your code:

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...