dongwen9051 2011-03-08 11:46
浏览 17
已采纳

递归菜单问题

I have been messing around with recursion a few hours and just cant get it right!

I want my menu structure to be:

<ul>
   <li> menu 1 <ul>
      <li> menu 1.1 <ul>
         <li> menu 1.1.1 </li>
      </ul>
   </ul>
<li> menu 2 </li>
<li> menu 3 <ul>
   <li> menu 3.1 </li>
</ul>
<li> menu 4 </li>
</ul>
</li>

but i just cant get it right.

    print '<ul>';
    display_children(0, 0);
function display_children($parent, $level) {
   // retrieve all children of $parent

   $result = mysql_query('SELECT * FROM kategori WHERE parent="'.$parent.'";');

   while ($row = mysql_fetch_array($result)) {

       if($row['parent']!=0)
            print '<ul>';
       print '<li>';

       // indent and display the title of this child
       echo $row['navn']."
";

       // call this function again to display this
       // child's children
       display_children($row['id'], $level+1);

   }

        print '</li>';
           print '</ul>';

as it is now, it prints the right structure but poops out a crapton of missplaced <ul> and <il>

my top level menu items have 0 as parent (to indicate that they are the absolut top) and its children have their respective parents id as parent attribut.

i tried to check if the current run through of the function (lets say we were at menu 1.1.1) had parent 0 which, to my surprise, was true as i would have thought the current run through would have have parent "menu 1.1" instead of "menu 1"

Any one able to help me out here? I have been staring at that damn code for waaay to long and if you have a quick fix to how i can get this code snippet to insert UL and IL the right places i would be most grateful!

Thanks in advance, Rasmus

  • 写回答

3条回答 默认 最新

  • dousui6488 2011-03-08 12:00
    关注

    display_children is perhaps not the best name, something like display_menu_level might be more descriptive. Think about what it has to do: display a <ul>, then a number of children, then a </ul>. Each child is an <li>, the "navn" text, a submenu, and an </li>. Just reflect that structure in code, something like this:

    function display_menu_level($parent, $level) {
        $results = mysql_query('SELECT * FROM kategori WHERE parent="'.$parent.'";');
    
        // Don't print an empty list ("<ul></ul>")
        if (mysql_num_rows($results) == 0) return;
    
        // Start the menu
        echo '<ul>';
    
        // Display children
        while ($row = mysql_fetch_array($result)) {
            // Start this child
            print '<li>';
    
            // indent and display the title of this child
            echo $row['navn']."
    ";
    
            // call this function again to display this
            // child's children
            display_children($row['id'], $level+1);
    
            // Close this child
            print '</li>';
        }
    
        // Close the menu
        print '</ul>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?