如何将 optgroup 添加到下拉循环?

发布于 2024-12-15 09:15:53 字数 608 浏览 1 评论 0原文

我有一个循环,在下拉框中显示变量列表(基于过滤的类别)。我想从我的表格中添加选项组(使用子类别)。如何将其添加到我的循环的一部分?

在下面的示例中,类别是“国家”,子类别是“大陆”。我希望将这些国家/地区列在相应的大陆选择组中。

<select id="country">
  <option value="">Select</option>

 <?php
     $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                              FROM constants WHERE category='Countries'
                              ORDER BY display");
     while ($row = $c_sth->fetch ()) {
       echo '<option value="'.$row['value'].'">'.$row['display'].</option>'."\n";
     }
 ?>

</select>

I have a loop that displays a list of variables in a dropdown box (based on the filtered category). I'd like to add in option groups (using subcategories) from my table. How can I add this to part of my loop?

In the example below, the category is "countries" and subcategory is "continents". I'd like the countries to be listed within the appropriate continent optgroup.

<select id="country">
  <option value="">Select</option>

 <?php
     $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                              FROM constants WHERE category='Countries'
                              ORDER BY display");
     while ($row = $c_sth->fetch ()) {
       echo '<option value="'.$row['value'].'">'.$row['display'].</option>'."\n";
     }
 ?>

</select>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2024-12-22 09:15:53
<?php
 $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                          FROM constants WHERE category='Countries'
                          ORDER BY subcategory, display"); # group by category first
 $current_subcategory = "";
 while ($row = $c_sth->fetch ()) {
   if ($row["subcategory"] != $current_subcategory) { # if category has changed
       if ($current_subcategory != "") { # if there was already a category active
           echo "</optgroup>"; # close it
       }
       echo '<optgroup label="'.$row['subcategory'].'">'; # open a new group
       $current_subcategory = $row['subcategory'];
   }
   echo '<option value="'.$row['value'].'">'.$row['display'].</option>'."\n';
 }
 echo "</optgroup>"; # close the final group
?>
<?php
 $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                          FROM constants WHERE category='Countries'
                          ORDER BY subcategory, display"); # group by category first
 $current_subcategory = "";
 while ($row = $c_sth->fetch ()) {
   if ($row["subcategory"] != $current_subcategory) { # if category has changed
       if ($current_subcategory != "") { # if there was already a category active
           echo "</optgroup>"; # close it
       }
       echo '<optgroup label="'.$row['subcategory'].'">'; # open a new group
       $current_subcategory = $row['subcategory'];
   }
   echo '<option value="'.$row['value'].'">'.$row['display'].</option>'."\n';
 }
 echo "</optgroup>"; # close the final group
?>
心在旅行 2024-12-22 09:15:53
<select id="country">
  <option value="">Select</option>

 <?php
     $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                              FROM constants WHERE category='Countries'
                              ORDER BY subcategory, display");

     $lastgroup = "";
     while ($row = $c_sth->fetch ()) {
       if ($lastgroup != $row['subcategory']) 
         echo '<optgroup label="'.$row['subcategory'].'">\n';
       echo '<option value="'.$row['value'].'">'.$row['display'].'</option>\n';
       if ($lastgroup != $row['subcategory']) 
         echo '</optgroup>\n';
       $lastgroup = $row['subcategory'];
     }
 ?>

</select>
<select id="country">
  <option value="">Select</option>

 <?php
     $c_sth = $c_dbh->query ("SELECT category, subcategory, display, value
                              FROM constants WHERE category='Countries'
                              ORDER BY subcategory, display");

     $lastgroup = "";
     while ($row = $c_sth->fetch ()) {
       if ($lastgroup != $row['subcategory']) 
         echo '<optgroup label="'.$row['subcategory'].'">\n';
       echo '<option value="'.$row['value'].'">'.$row['display'].'</option>\n';
       if ($lastgroup != $row['subcategory']) 
         echo '</optgroup>\n';
       $lastgroup = $row['subcategory'];
     }
 ?>

</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文