gpt4 book ai didi

php - 具有动态下拉菜单的 Optgroups

转载 作者:行者123 更新时间:2023-11-30 00:53:32 25 4
gpt4 key购买 nike

大家好,我有下表

--------------------------------------------
|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,但是有人可以指出我正确的方向吗?

谢谢

最佳答案

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

WORKING DEMO JsFiddle Html Output

关于php - 具有动态下拉菜单的 Optgroups,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20774740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com