大家好,我有以下表格
--------------------------------------------
|ID|point_name|point_address|point_category|
|01|bobs |7362 178 st |House |
|02|Greegs |1123 118 Ave |Store |
And so on
--------------------------------------------
我想在组合框中使用optgroup功能,以便用户可以在搜索时获得分类结果。
到目前为止我的代码:
<select id="combobox">
<?php
$end_point = "SELECT * FROM points";
$q_end_point = $conn->query($end_point);
$q_end_point->setFetchMode(PDO::FETCH_BOTH);
while ($row = $q_end_point->fetch()) {
echo "<option value='" . $row['point_address'] . "'>" . $row['point_name'] . "</option>";
}
?>
我不知道如何添加optgroup但是,有人能指出我正确的方向吗?
由于
答案 0 :(得分:0)
<?php
/* First you need to format your array like bellow*/
$array = array(
array(
'category' => "House",
'result' => array(
1,
2,
3
)
),
array(
'category' => "Store",
'result' => array(
4,
5,
6
)
)
);
?>
<select>
<?php foreach($array as $value): ?>
<optgroup label="<?php echo $value['category'];?>">
<?php foreach($value[ 'result'] as $value): ?>
<option value="<?php echo $value;?>">
<?php echo $value;?>
</option>
<?php endforeach ?>
</optgroup>
<?php endforeach ?>
</select>