我正在开发一个下拉菜单,它使用 HTML optgroups 作为员工所属的组名称.这是 MySQL 查询和输出:
<前>mysql> SELECT 雇员.emp_id,employees.empname,employees.grp_id,groups.groupname FROM 雇员离开加入组在雇员.grp_id = groups.grp_id 上按组名 asc 排序;+--------+------------+--------+-----------+|emp_id |员工姓名 |grp_id |组名 |+--------+------------+--------+-----------+|20 |员工 2 |13 |第 1 组 ||19 |员工 1 |13 |第 1 组 ||21 |员工 3 |14 |第 2 组 |+--------+------------+--------+-----------+3 行(0.00 秒)唯一的问题是,我最难弄清楚如何让 optgroup 正常工作.我已经尝试了无数次,它真的开始让我感到沮丧.以下几乎是我想要的输出(示例):
基本上,optgroup 需要是groupname",选项name"应该是emp_id",动作option"(下拉项)是empname".
我希望这是可以做到的,但真的不确定.这是我拥有的功能,但效果不佳:
function getDynGrpList() {全球 $db;//$query = "SELECT * FROM员工 ORDER BY grp_id desc;";$query = "选择雇员.emp_id,employees.empname,employees.grp_id,groups.groupname FROM 雇员离开加入组雇员.grp_id = groups.grp_id 按组名 asc 排序;";$employees = $db->GetAll($query);$groups = array();而 ($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() {全球 $db;//$query = "SELECT * FROM员工 ORDER BY grp_id desc;";$query = "选择雇员.emp_id,employees.empname,employees.grp_id,groups.groupname FROM 雇员离开加入组雇员.grp_id = groups.grp_id 按组名 asc 排序;";$employees = $db->GetAll($query);$groups = array();而 ($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() {全球 $db;$query = "选择雇员.emp_id,employees.empname,employees.grp_id,groups.groupname FROM 雇员离开加入组雇员.grp_id = groups.grp_id 按组名 asc 排序;";$employees = $db->GetAll($query);$groups = array();foreach ($employees 作为 $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;}
这两个 for 循环没有嵌套在你的代码中:
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 } ?>
结果是首先创建了所有 opt 组,然后添加了最后一组的员工(因为 $label
和 $opt
在循环后也可用完成).
你必须嵌套循环(使用替代语法控制结构):
$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'];}
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;
}
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'];
}
这篇关于PHP:使用 optgroup 动态下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!