我有一个下拉菜单,带有一个PHP'foreach',我循环来填充选择选项。这很好。但是,我想要做的是使用“optgroup”选项的各种子标签。在我返回的数组中,我有一个“蓝色”,“绿色”和“红色”的“类型”。我如何根据$ album ['type']将选项分成多个组?选择下拉PHP foreach循环与'optgroup'选项
这是我的代码:
$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>
谢谢!这很好用! – Chris