我有以下代码填充选择列表,但是每个选项('link_title')在列表中都有一个 optgroup 标题,而不是将选项分组到 optgroup 标题中:
<?php
$query = mysql_query("SELECT * from link, link_category WHERE link.link_category_fk = link_category.link_category_pk ORDER BY link_category_fk, link_title ASC");
$current_subcategory = "";
while ($row = mysql_fetch_array($query)){
if ($row["link_category_name"] != $current_subcategory) {
if ($current_subcategory != "") {
echo "</optgroup>";
}
echo '<optgroup label="'.$row['link_category_name'].'">';
$current_subcategory = $row['subcategory'];
}
echo '<option value="'.$row['link'].'">'.$row['link_title'].'</option>'."\n";
}
echo "</optgroup>";
?>
</select>