Hello, there is a plugin of the three-level menu. I want to implement it in php, it does not work. I have a Catalog table with the fields id, parent, name. I could only do a two-level process, but I cannot register the third cycle, recursion is also not solved here, because it does not prescribe the correct location of tags

$tree_res = mysql_query("SELECT * FROM catalog WHERE parent= '0' AND checked='1' ORDER BY name"); while($menu_tree = mysql_fetch_assoc($tree_res)) { ?> <li><a href="/catview.php?id=<?php echo $menu_tree['id'] ?>"><?php echo $menu_tree['name'] ?></a> <?php $sub_tree_res = mysql_query("SELECT * FROM catalog WHERE parent= ' ".$menu_tree['id']." ' AND checked='1' ORDER BY name "); if (!empty($sub_tree_res)) { ?> <ul> <?php while($sub_menu_tree = mysql_fetch_assoc($sub_tree_res)) { ?> <li><a href="/catalogview.php?id=<?php echo $sub_menu_tree['id'] ?>"><?php echo $sub_menu_tree['name']; $res = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tovar WHERE Subdivision='$sub_menu_tree[id]' and checked='1'")); echo " <b>($res[0])</b>"; ?></a></li> <?php } ?></ul> <?php } } ?> </ul> 
  • I do not see your recursion, and without it you can not cope here. - Grimon

1 answer 1

I got your menu like this. True, it is necessary to refine. I have nothing to check

 function menu($id) { $tree_res = mysql_query("SELECT * FROM catalog WHERE parent={$id} AND checked=1 ORDER BY name"); while ($menu_tree = mysql_fetch_assoc($tree_res)) { echo "<li><a href='/catview.php?id={$menu_tree['id']}'>"; echo "{$menu_tree['name']}</a><ul>"; menu($menu_tree['id']); echo "</ul></li>"; } return; } echo "<ul>"; menu(0); echo "</ul>"; 

It turns out the output of the 2nd level. The third is simply after each withdrawal, another reference to the function of checking the goods.

PS And if you have different requests at all: on the first level, and on the second, then each request should be put in a separate function.