我尝试从包含主要项目及其子项目的数据库中创建一个下拉列表。我希望主要项目成为 optgroup 标签,子项目成为选项项目。
<select>
<optgroup label="$rs_pa['Name']">
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
</optgroup>
<optgroup label="$rs_pa['Name']">
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
<option value="$rs_ch['Link']">$rs_ch['Name']</option>
</optgroup>
</select>
以上只是显示每个变量应适合的位置的方案。
$rs_pa['Name] - 是父项的名称。
$rs_ch['Name] - 是子项的名称。
$rs_ch['Link'] - 是子项的链接。
这是我现在拥有的代码:
$mysql_table_items = "sys_menu_top";
$mysql_table_options = "sys_options";
// SQL QUERY
$mysql_result_number = mysql_query("SELECT VALUE FROM $mysql_table_options WHERE Name LIKE 'nav_menu_elements_on_line_usr'") or die($myQuery . "<br/><br/>" . mysql_error());
$mysql_select_parent = "SELECT * FROM $mysql_table_items WHERE `Parent` = '0' AND Type LIKE 'top' AND Active = 1 ORDER BY `Order` ASC";
$mysql_select_child = "SELECT * FROM $mysql_table_items WHERE `Parent` = '" . $rs_pa['ID'] . "' AND Active = 1 ORDER BY `Order` ASC";
// SQL RESULT
$mysql_result_number = mysql_fetch_array($mysql_result_number, MYSQL_ASSOC) or die("Error: Query Failed! " . mysql_error());
$mysql_result_parent = mysql_query($mysql_select_parent) or die("Error: Query Failed! " . mysql_error());
$mysql_result_child = mysql_query($mysql_select_child) or die("Error: Query Failed! " . mysql_error());
$output .= "<select>";
while ($rs_pa = mysql_fetch_array($mysql_result_parent)) {
$output .= "<optgroup label='" . $rs_pa['Name'] . "'>";
while ($rs_ch = mysql_fetch_array($mysql_result_child)) {
$output .= "<option value='" . $rs_ch['Link'] . "'>" . $rs_ch['Name'] . "</option>";
}
$output .= "</optgroup>";
}
$output .= "</select>";
echo $output;
问题是这显示不正确..我尝试了一切,但我对php不太好..谢谢你的帮助..