Javascript 选择在while循环中嵌入Optgroup的组
我有一个while循环,它获取Javascript 选择在while循环中嵌入Optgroup的组,javascript,php,Javascript,Php,我有一个while循环,它获取agentspec表中的所有值,并将它们设置为select字段中的一个选项 agentspec表中的我的值按category字段分组,我想使用category字段作为我的optgroup标签 这就是我尝试过的。它当前输出所有类别字段值,后跟所有规范值 例如 我希望它根据在类别字段中设置的组输出规格值 例如 代码: $st=DBase::singleton() ->预备( “选择*”。 '来自'agentspec`'。 “限值0,30”); $option=''; $
agentspec
表中的所有值,并将它们设置为select字段中的一个选项
agentspec
表中的我的值按category
字段分组,我想使用category
字段作为我的optgroup标签
这就是我尝试过的。它当前输出所有类别
字段值,后跟所有规范
值
例如
我希望它根据在类别
字段中设置的组输出规格
值
例如
代码:
$st=DBase::singleton()
->预备(
“选择*”。
'来自'agentspec`'。
“限值0,30”);
$option='';
$optgroup='';
如果($st->execute())
{
while($row=$st->fetch(PDO::fetch_OBJ))
{
$id=$row->id;
$cat=$row->类别;
$spec=htmlspecialchars($row->spec);
$desc=htmlspecialchars($row->desc);
$optgroup.='';
$option.='.$spec.','.$desc.';
}
}
?>
选择一个专业领域
选项是optgroup的子元素,因此您必须执行以下操作
这种原油:
代码段的重写版本:
<?php
$st = DBase::singleton()
->prepare(
'select * ' .
'from `agentspec` ' .
'limit 0,30');
$optHtml= '';
$optgroups = array();
if ($st->execute())
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$id = $row->id;
$cat = $row->category;
$spec = htmlspecialchars($row->spec);
$desc = htmlspecialchars($row->desc);
if (!array_key_exists($cat, $optgroups)) {
$optgroups[$cat] = "";
}
$optgroups[$cat].='<option value = "agents/'.$id.'/themes">'.$spec.' , '.$desc.'</option>';
}
foreach($optgroups as $label=>$optionsHtml) {
$optHtml.= '<optgroup label="'.$label.'">'.$optionsHtml.'</optgroup>';
}
}
?>
<select id="selectbox" name="" class=form-control>
<option selected="selected">Select a Specialist Area</option>
<?php echo $optHtml; ?>
</select>
选择一个专业领域
这是一个简单的控件中断,您想在这里编程,所以请仔细阅读。非常棒!非常感谢
Category: Farmer
Spec: Grain
Spec: Sand
Spec: Fruit
Category: Colour
Spec: Red
Spec: Blue
$st = DBase::singleton()
->prepare(
'select * ' .
'from `agentspec` ' .
'limit 0,30');
$option = '';
$optgroup = '';
if ($st->execute())
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$id = $row->id;
$cat = $row->category;
$spec = htmlspecialchars($row->spec);
$desc = htmlspecialchars($row->desc);
$optgroup .= '<optgroup label= '.$cat.'></optgroup>';
$option .= '<option value = "agents/'.$id.'/themes">'.$spec.' , '.$desc.'</option>';
}
}
?>
<select id="selectbox" name="" class=form-control>
<option selected="selected">Select a Specialist Area
</option>
<?php echo $optgroup;?>
<?php echo $option;?>
</select>
<?php
$st = DBase::singleton()
->prepare(
'select * ' .
'from `agentspec` ' .
'limit 0,30');
$optHtml= '';
$optgroups = array();
if ($st->execute())
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$id = $row->id;
$cat = $row->category;
$spec = htmlspecialchars($row->spec);
$desc = htmlspecialchars($row->desc);
if (!array_key_exists($cat, $optgroups)) {
$optgroups[$cat] = "";
}
$optgroups[$cat].='<option value = "agents/'.$id.'/themes">'.$spec.' , '.$desc.'</option>';
}
foreach($optgroups as $label=>$optionsHtml) {
$optHtml.= '<optgroup label="'.$label.'">'.$optionsHtml.'</optgroup>';
}
}
?>
<select id="selectbox" name="" class=form-control>
<option selected="selected">Select a Specialist Area</option>
<?php echo $optHtml; ?>
</select>