Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择在while循环中嵌入Optgroup的组_Javascript_Php - Fatal编程技术网

Javascript 选择在while循环中嵌入Optgroup的组

Javascript 选择在while循环中嵌入Optgroup的组,javascript,php,Javascript,Php,我有一个while循环,它获取agentspec表中的所有值,并将它们设置为select字段中的一个选项 agentspec表中的我的值按category字段分组,我想使用category字段作为我的optgroup标签 这就是我尝试过的。它当前输出所有类别字段值,后跟所有规范值 例如 我希望它根据在类别字段中设置的组输出规格值 例如 代码: $st=DBase::singleton() ->预备( “选择*”。 '来自'agentspec`'。 “限值0,30”); $option=''; $

我有一个while循环,它获取
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>