我正在编写一个多级菜单,下面的代码只能获得父类和
是小孩,我需要让孩子等孩子等等
请帮帮我::
$resultMainMenu = mysql_query("SELECT menu.menu_id,menu.menu_name,menu.parent_id FROM
menu,menu_users WHERE menu.parent_id=0
AND menu.something='x'
AND menu_users.menu_id = menu.menu_id AND menu_users.user_type=1 ") or die(mysql_error());
while($row = mysql_fetch_array($resultMainMenu)){
echo $row['menu_name'] . '<br />'; // echo main menu
$resultSubmenu = mysql_query("SELECT * FROM menu WHERE parent_id='". $row['menu_id']."' AND menu.something='x'") or die(mysql_error());
if(mysql_num_rows($resultSubmenu) >= 1){
while($rowSub = mysql_fetch_array($resultSubmenu)){
echo ' -- ' . $rowSub['menu_name'] . '<br />'; // echo sub menu
}
}
}
答案 0 :(得分:0)
您可以通过重写代码来完成此操作,如下面的递归函数。
$resultMainMenu = mysql_query("SELECT menu.menu_id,menu.menu_name,menu.parent_id FROM
menu,menu_users WHERE menu.parent_id=0
AND menu.something='x'
AND menu_users.menu_id = menu.menu_id AND menu_users.user_type=1 ") or die(mysql_error());
recur($resultMainMenu, "");
function recur($result, $preFormat) {
if(mysql_num_rows($result) >= 1){
while($row = mysql_fetch_array($result)){
echo $preFormat . $row['menu_name'] . '<br />'; // echo main menu
$newResult = mysql_query("SELECT * FROM menu WHERE parent_id='". $row['menu_id']."'
AND menu.something='x'") or die(mysql_error());
recur($newResult, $preFormat . " -- ");
}
}
}