<small id='TooKf'></small><noframes id='TooKf'>

    1. <i id='TooKf'><tr id='TooKf'><dt id='TooKf'><q id='TooKf'><span id='TooKf'><b id='TooKf'><form id='TooKf'><ins id='TooKf'></ins><ul id='TooKf'></ul><sub id='TooKf'></sub></form><legend id='TooKf'></legend><bdo id='TooKf'><pre id='TooKf'><center id='TooKf'></center></pre></bdo></b><th id='TooKf'></th></span></q></dt></tr></i><div id='TooKf'><tfoot id='TooKf'></tfoot><dl id='TooKf'><fieldset id='TooKf'></fieldset></dl></div>
      <legend id='TooKf'><style id='TooKf'><dir id='TooKf'><q id='TooKf'></q></dir></style></legend>
        <bdo id='TooKf'></bdo><ul id='TooKf'></ul>
    2. <tfoot id='TooKf'></tfoot>
      1. 使用“optgroup"选项选择下拉 PHP foreach 循环

        时间:2023-09-21
          <bdo id='8EPij'></bdo><ul id='8EPij'></ul>
        • <i id='8EPij'><tr id='8EPij'><dt id='8EPij'><q id='8EPij'><span id='8EPij'><b id='8EPij'><form id='8EPij'><ins id='8EPij'></ins><ul id='8EPij'></ul><sub id='8EPij'></sub></form><legend id='8EPij'></legend><bdo id='8EPij'><pre id='8EPij'><center id='8EPij'></center></pre></bdo></b><th id='8EPij'></th></span></q></dt></tr></i><div id='8EPij'><tfoot id='8EPij'></tfoot><dl id='8EPij'><fieldset id='8EPij'></fieldset></dl></div>

          <small id='8EPij'></small><noframes id='8EPij'>

            <tbody id='8EPij'></tbody>

              <tfoot id='8EPij'></tfoot>
              <legend id='8EPij'><style id='8EPij'><dir id='8EPij'><q id='8EPij'></q></dir></style></legend>

                  本文介绍了使用“optgroup"选项选择下拉 PHP foreach 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个带有 PHP 'foreach' 的下拉菜单,我循环遍历它以填充选择选项.这很好用.但是,我想要做的是使用optgroup"选项拥有各种子标签.在我返回的数组中,我有一个蓝色"、绿色"和红色"的类型".如何根据 $album['type'] 将选择选项分组?

                  I have a dropdown menu with a PHP 'foreach' that I loop through to populate the select options. This works well. However, what I'd like to do is have various sub labels using the "optgroup" option. In my returned array, I have a "type" of "blue", "green" and "red". How can I split the select options up into groups based on the $album['type']?

                  这是我的代码:

                  $query = mysql_query("SELECT * FROM background_albums);
                  $albums = array();
                  while($row = mysql_fetch_assoc($query)) {
                      array_push($albums, $row);
                  }
                  
                  <select name="backgroundList" id="backgroundList">
                    <? foreach($albums as $album) { ?>
                      <option value="<?=($row['id']);?>"><?=$row['title'];?></option>
                    <? } ?>
                  </select>
                  

                  这是我想在 foreach 循环中实现的那种:

                  This is the kind of thing I'd like to achieve within the foreach loop:

                  if ($album['type'] == 'green')...
                  
                  <optgroup label="Green Backgrounds">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                  </optgroup>
                  
                  if ($album['type'] == 'blue')...
                  
                  <optgroup label="Blue Backgrounds">
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                  </optgroup>
                  

                  推荐答案

                  尝试

                  <select name="backgroundList" id="backgroundList">
                  <?php
                  $album_type = '';
                  foreach ($albums as $album) {
                    if ($album_type != $album['type']) {
                      if ($album_type != '') {
                        echo '</optgroup>';
                      }
                      echo '<optgroup label="'.ucfirst($album['type']).' Backgrounds">';
                    }
                    echo '<option value="'.$album['id'].'">'.htmlspecialchars($album['title']).'</option>';
                    $album_type = $album['type'];    
                  }
                  if ($album_type != '') {
                    echo '</optgroup>';
                  }
                  ?>
                  </select>
                  

                  这篇关于使用“optgroup"选项选择下拉 PHP foreach 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:迭代多维数组 下一篇:如何在php中删除数组键名称中的空格?

                  相关文章

                  最新文章

                  1. <i id='12UJs'><tr id='12UJs'><dt id='12UJs'><q id='12UJs'><span id='12UJs'><b id='12UJs'><form id='12UJs'><ins id='12UJs'></ins><ul id='12UJs'></ul><sub id='12UJs'></sub></form><legend id='12UJs'></legend><bdo id='12UJs'><pre id='12UJs'><center id='12UJs'></center></pre></bdo></b><th id='12UJs'></th></span></q></dt></tr></i><div id='12UJs'><tfoot id='12UJs'></tfoot><dl id='12UJs'><fieldset id='12UJs'></fieldset></dl></div>
                    • <bdo id='12UJs'></bdo><ul id='12UJs'></ul>
                      <legend id='12UJs'><style id='12UJs'><dir id='12UJs'><q id='12UJs'></q></dir></style></legend>
                      <tfoot id='12UJs'></tfoot>

                      <small id='12UJs'></small><noframes id='12UJs'>