Optimering af SQL-forespørgsler
Hej Eksperter.Jeg har flg. kode til en dropdown menu. Dog synes jeg der bruges for mange forespørgsler - som stiger i takt med oprettede menupunkter:
// ============================== Main Menu Start ============================== //
$msql = dbquery("SELECT * FROM ".DB_MENUS." WHERE menu_cat='0' ORDER BY menu_order");
if (dbrows($msql) != 0) {
echo "<ul id='nav'>";
while ($mdata = dbarray($msql)) {
if (checkgroup($mdata['menu_access'])) {
$link_target = ($mdata['menu_window'] == "1" ? " target='_blank'" : "");
if(dbrows(dbquery("SELECT menu_cat FROM ".DB_MENUS." WHERE menu_cat='".$mdata['menu_id']."'"))) {
$div_class = " class='down'";
} else {
$div_class = "";
}
if ($mdata['menu_name'] !== "---" && $mdata['menu_link'] == "---") {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata['menu_link'])) {
echo "<li class='top'><div".$div_class."><a href='".$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
} else {
echo "<li class='top'><div".$div_class."><a href='".BASEDIR.$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
}
} else if ($mdata['menu_name'] !== "---" && $mdata['menu_link'] == "") {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata['menu_link'])) {
echo "<li class='top'><div".$div_class."><a href='".$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
} else {
echo "<li class='top'><div".$div_class."><a href='".BASEDIR.$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
}
} else if ($mdata['menu_name'] == "---" && $mdata['menu_link'] == "---") {
echo "<li class='top'><hr />\n";
} else {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata['menu_link'])) {
echo "<li class='top'><div".$div_class."><a href='".$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
} else {
echo "<li class='top'><div".$div_class."><a href='".BASEDIR.$mdata['menu_link']."'".$link_target." class='top_link'>".parseubb($mdata['menu_name'], "b|i|u|img|color")."</a></div>\n";
}
}
// ============================== Sub Menu 1 Start ============================== //
$msql1 = dbquery("SELECT * FROM ".DB_MENUS." WHERE menu_cat='".$mdata['menu_id']."' ORDER BY menu_order");
if (dbrows($msql1) != 0) {
echo "<ul class='sub'>";
while ($mdata1 = dbarray($msql1)) {
if (checkgroup($mdata1['menu_access'])) {
$link_target = ($mdata1['menu_window'] == "1" ? " target='_blank'" : "");
if(dbrows(dbquery("SELECT menu_cat FROM ".DB_MENUS." WHERE menu_cat='".$mdata1['menu_id']."'"))) {
$link_class = " class='fly'";
} else {
$link_class = "";
}
if ($mdata1['menu_name'] !== "---" && $mdata1['menu_link'] == "---") {
echo "<li><b>".parseubb($mdata1['menu_name'], "b|i|u|left|center|right|img|color")."</b>\n";
} else if ($mdata1['menu_name'] == "---" && $mdata1['menu_link'] == "---") {
echo "<li><hr />\n";
} else {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata1['menu_link'])) {
echo "<li><a href='".$mdata1['menu_link']."'".$link_target."".$link_class.">".parseubb($mdata1['menu_name'], "b|i|u|left|center|right|img|color")."</a>\n";
} else {
echo "<li><a href='".BASEDIR.$mdata1['menu_link']."'".$link_target."".$link_class.">".parseubb($mdata1['menu_name'], "b|i|u|left|center|right|img|color")."</a>\n";
}
}
// ============================== Sub Menu 2 Start ============================== //
$msql2 = dbquery("SELECT * FROM ".DB_MENUS." WHERE menu_cat='".$mdata1['menu_id']."' ORDER BY menu_order");
if (dbrows($msql2) != 0) {
echo "<ul>";
while ($mdata2 = dbarray($msql2)) {
if (checkgroup($mdata2['menu_access'])) {
$link_target = ($mdata2['menu_window'] == "1" ? " target='_blank'" : "");
if(dbrows(dbquery("SELECT menu_cat FROM ".DB_MENUS." WHERE menu_cat='".$mdata2['menu_id']."'"))) {
$link_class = " class='fly'";
} else {
$link_class = "";
}
if ($mdata2['menu_name'] !== "---" && $mdata2['menu_link'] == "---") {
echo "<li><b>".parseubb($mdata2['menu_name'], "b|i|u|left|center|right|img|color")."</b>\n";
} else if ($mdata2['menu_name'] == "---" && $mdata2['menu_link'] == "---") {
echo "<li><hr />\n";
} else {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata2['menu_link'])) {
echo "<li><a href='".$mdata2['menu_link']."'".$link_target."".$link_class.">".parseubb($mdata2['menu_name'], "b|i|u|left|center|right|img|color")."</a>\n";
} else {
echo "<li><a href='".BASEDIR.$mdata2['menu_link']."'".$link_target."".$link_class.">".parseubb($mdata2['menu_name'], "b|i|u|left|center|right|img|color")."</a>\n";
}
}
// ============================== Sub Menu 3 Start ============================== //
$msql3 = dbquery("SELECT * FROM ".DB_MENUS." WHERE menu_cat='".$mdata2['menu_id']."' ORDER BY menu_order");
if (dbrows($msql3) != 0) {
echo "<ul>";
while ($mdata3 = dbarray($msql3)) {
if (checkgroup($mdata3['menu_access'])) {
$link_target = ($mdata3['menu_window'] == "1" ? " target='_blank'" : "");
if ($mdata3['menu_name'] !== "---" && $mdata3['menu_link'] == "---") {
echo "<li><b>".parseubb($mdata3['menu_name'], "b|i|u|left|center|right|img|color")."</b>\n";
} else if ($mdata3['menu_name'] == "---" && $mdata3['menu_link'] == "---") {
echo "<li><hr /></li>\n";
} else {
if (preg_match("!^(ht|f)tp(s)?://!i", $mdata3['menu_link'])) {
echo "<li><a href='".$mdata3['menu_link']."'".$link_target.">".parseubb($mdata3['menu_name'], "b|i|u|left|center|right|img|color")."</a></li>\n";
} else {
echo "<li><a href='".BASEDIR.$mdata3['menu_link']."'".$link_target.">".parseubb($mdata3['menu_name'], "b|i|u|left|center|right|img|color")."</a></li>\n";
}
}
// ============================== Sub Menu 3 End ============================== //
}
}
echo "</ul>\n";
}
echo "</li>\n";
// ============================== Sub Menu 2 End ============================== //
}
}
echo "</ul>\n";
}
echo "</li>\n";
// ============================== Sub Menu 1 End ============================== //
}
}
echo "</ul>\n";
}
echo "</li>\n";
// ============================== Main Menu End ============================== //
}
}
echo "</ul>\n";
}
Jeg har ladet mig vide at det skulle være de 4 while løkker der laver de mange forespørgsler, men jeg kan desværre bare ikke se hvordan man evt. kan undlade dem - og så stadig at menuen virker.
Er her mon et skarpt hovede der kan hjælpe ?
Mvh.
Buxxy.