Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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
选择带有“optgroup”选项的下拉式PHP foreach循环_Php_Select_Foreach_Optgroup - Fatal编程技术网

选择带有“optgroup”选项的下拉式PHP foreach循环

选择带有“optgroup”选项的下拉式PHP foreach循环,php,select,foreach,optgroup,Php,Select,Foreach,Optgroup,我有一个带有PHP“foreach”的下拉菜单,我通过它循环填充选择选项。这很有效。但是,我想做的是使用optgroup选项创建各种子标签。在返回的数组中,我有一种蓝色、绿色和红色。如何根据$album['type']将选择选项拆分为组 这是我的代码: $query = mysql_query("SELECT * FROM background_albums); $albums = array(); while($row = mysql_fetch_assoc($query)) { ar

我有一个带有PHP“foreach”的下拉菜单,我通过它循环填充选择选项。这很有效。但是,我想做的是使用optgroup选项创建各种子标签。在返回的数组中,我有一种蓝色、绿色和红色。如何根据$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循环中实现的事情:

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>

检查类型是否与以前的值不同,然后发出相应的标记

按类型对查询排序:

$query = mysql_query("SELECT * FROM background_albums order by type"); //ordered query

[...]

$type = null;

foreach ($albums as $album) {
  if ($album['type'] != $type) {  // type changed
    if ($type !== null) {
      echo '</optgroup>';  // close previous optgroup
    }
    $type = $album['type'];
    echo '<optgroup label="' . htmlspecialchars($type) . '">';  // start optgroup
  }

  [...]
}

echo '</optgroup>';  // close last 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解决方案

这段代码与tagrin0非常相似,但我想我会提供更多的注释,以帮助其他可能希望清楚了解如何在不使用代码的情况下实现这一点的Noob

希望它能帮助别人

这是表格结构

opt|u id | grp|u id | optName | grpName | opttabbrv

echo '<select id="xxx" class="xxx"> select">';
echo '<option >Select an instance...</option>';

$dbc = mysqli_connect('localhost','xxx','xxx','xxx') or die('Error connecting to MySQL server.');

// run your query, something like
// "SELECT <optgroup>_ID, <option>_ID, <optgroup>_Name, <ption>_Name FROM [mysql table this data is stored in] ORDER BY groupID, optionID"

$select = "SELECT * FROM ci_opt ORDER BY grp_id, opt_id";

    $data  = @mysqli_query($dbc, $select) or die(mysql_error());

    // <optgroup> of the previous <option>
    $previous = "";

    // variable to set the first group
    $first_group = true;

    while($row = mysqli_fetch_array($data)) {

        // if this <option> changed <optgroup> from the previous one,
        // then add a new <optgroup>
        if ($row['grpName'] != $previous) {
            if (!$first_group) {
                echo '</optgroup>';
            } else {
                $first_group = false;
            }
            echo '<optgroup label="' . $row['grpName'] . '">';
            $previous = $row['grpName'];
        }

        // echo the current <option>
        // Note: I've also prepared the [onclick] for linking the select to an event

        echo '<option id="'. $row['optAbbrv'] .'" onclick="javascript:void()">' . ucwords($row['optName']) . '</option>';

    }
    // close the last <optgroup> tag
    echo '</optgroup>';
    // close the last <select> tag
    echo '</select>';
    // close the connection
    mysqli_close($dbc);

//end php

此方法不需要对数组进行排序

<select>
    <?php
    <!--  getting the groups to be one unique array -->
    $group= array_values(array_unique(array_column($album, 'type')));
             for ($i=0; $i < count($group); $i++) : ?>
    <!--  for N group print  -->
    <optgroup label="<?=$album[$i];?>">
        <!-- loop trough the hole array to check if you belong to the group -->
        <?php foreach ($album as $value): ?>
        <!-- if you belong then we print -->
        <?php if ($group_level[$i]==$value['type']): ?>
        <option>
            <?=$value['type'];?>
        </option>
        <?php endif ?>
        <?php endforeach ?>
    </optgroup>
    <?php endfor ?>
</select>

要使其正常工作,选择应按类型排序,对吗?