PHP:使用递归函数嵌套的菜单中,展开只有一些节点(不是所有的树) [英] PHP: nested menu with a recursive function, expand only some nodes (not all the tree)

查看:121
本文介绍了PHP:使用递归函数嵌套的菜单中,展开只有一些节点(不是所有的树)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的阵列,名为 $嵌套(这是一个漫长的,但我试图让一个COM prehensive情景):

I have this array, called $nested (it's a long one, but I tried to get a comprehensive scenario):

Array
(
    [1] => Array
        (
            [id] => 1
            [parent] => 0
            [title] => Page 1
        )

    [2] => Array
        (
            [id] => 2
            [parent] => 0
            [title] => Page 2
        )

    [3] => Array
        (
            [id] => 3
            [parent] => 0
            [title] => Page 3
        )

    [4] => Array
        (
            [id] => 4
            [parent] => 0
            [title] => Page 4
        )

    [5] => Array
        (
            [id] => 5
            [parent] => 0
            [title] => Page 5
        )

    [6] => Array
        (
            [id] => 6
            [parent] => 1
            [title] => Page 1-1
        )

    [7] => Array
        (
            [id] => 7
            [parent] => 1
            [title] => Page 1-2
        )

    [8] => Array
        (
            [id] => 8
            [parent] => 1
            [title] => Page 1-3
        )

    [9] => Array
        (
            [id] => 9
            [parent] => 2
            [title] => Page 2-1
        )

    [10] => Array
        (
            [id] => 10
            [parent] => 2
            [title] => Page 2-2
        )

    [11] => Array
        (
            [id] => 11
            [parent] => 2
            [title] => Page 2-3
        )

    [12] => Array
        (
            [id] => 12
            [parent] => 3
            [title] => Page 3-1
        )

    [13] => Array
        (
            [id] => 13
            [parent] => 3
            [title] => Page 3-2
        )

    [14] => Array
        (
            [id] => 14
            [parent] => 4
            [title] => Page 4-1
        )

    [15] => Array
        (
            [id] => 15
            [parent] => 6
            [title] => Page 1-1-1
        )

    [16] => Array
        (
            [id] => 16
            [parent] => 6
            [title] => Page 1-1-2
        )

    [17] => Array
        (
            [id] => 17
            [parent] => 6
            [title] => Page 1-1-3
        )

    [18] => Array
        (
            [id] => 18
            [parent] => 7
            [title] => Page 1-2-1
        )

    [19] => Array
        (
            [id] => 19
            [parent] => 7
            [title] => Page 1-2-2
        )

    [20] => Array
        (
            [id] => 20
            [parent] => 7
            [title] => Page 1-2-3
        )

    [21] => Array
        (
            [id] => 21
            [parent] => 9
            [title] => Page 2-1-1
        )

    [22] => Array
        (
            [id] => 22
            [parent] => 9
            [title] => Page 2-1-2
        )

    [23] => Array
        (
            [id] => 23
            [parent] => 9
            [title] => Page 2-1-3
        )

)

通过这个递归函数:

function recursive($parent, $array) {
    $has_children = false;
    foreach($array as $key => $value) {
        if ($value['parent'] == $parent) {       
            if ($has_children === false && $parent) {
                $has_children = true;
                echo '<ul>' ."\n";
            }
            echo '<li>' . "\n";
                echo '<a href="/page.php?id=' . $value['id'] . '">' . $value['title'] . '</a>' . " \n";
            echo "\n";
                recursive($key, $array);
            echo "</li>\n";
        }
    }
    if ($has_children === true && $parent) echo "</ul>\n";
}

<ul><?php echo recursive(0, $nested); ?></ul>

我很容易得到这样的输出:

I easily get this output:


        
  • 1
            

                  
    • 1-1页
                      

                            
      • 页1-1-1

      •                     
      • 页1-1-2

      •                     
      • 页1-1-3

      •                 
      • Page 1
        • Page 1-1
          • Page 1-1-1
          • Page 1-1-2
          • Page 1-1-3

                                
          • 页1-2-1

          •                     
          • 页1-2-2

          •                     
          • 页1-2-3

          •                 
          • Page 1-2-1
          • Page 1-2-2
          • Page 1-2-3

                        
          • 页2-1
                            

                                  
            • 页2-1-1

            •                     
            • 页2-1-2

            •                     
            • 页2-1-3

            •                 
            • Page 2-1
              • Page 2-1-1
              • Page 2-1-2
              • Page 2-1-3

                            
              • 3-1页

              •             
              • 3-2页

              •         
              • Page 3-1
              • Page 3-2

                            
              • 4-1页

              •         
              • Page 4-1

              到目前为止好。

              现在,我想不要在一次显示整个树,而是更深层次的去当一个页面/子页面的用户点击,像这样的:

              Now, I would like NOT to show the whole tree at once, but going deeper when the user clicks on a page/subpage, like this:

              网址: http://www.example.com/page.php ,初始状态(扩大与父母的所有项目= 0)

              URL: http://www.example.com/page.php, initial state ("expand" all items with parent = 0)

              • Page 1
              • Page 2
              • Page 3
              • Page 4
              • Page 5

              网址: http://www.example.com/page.php?id= 1 (扩大与父母所有项目= 1)

              URL: http://www.example.com/page.php?id=1 (expand all items with parent = 1)


                    
              • 1
                        

                              
                • 1-1页

                •             
                • 第1-2页

                •             
                • 1-3页

                •         
                • Page 1
                  • Page 1-1
                  • Page 1-2
                  • Page 1-3

                  网址: http://www.example.com/page.php?id= 6 (扩大与父= 6的所有项目)
                      


                            
                  • 1
                                

                                      
                    • 1-1页
                                                  

                                            
                      • 页1-1-1

                      •                     
                      • 页1-1-2

                      •                     
                      • 页1-1-3

                      •                 

                        URL: http://www.example.com/page.php?id=6 (expand all items with parent = 6)

                        • Page 1
                          • Page 1-1
                            • Page 1-1-1
                            • Page 1-1-2
                            • Page 1-1-3
                            • 和等

                              似乎是一个不可能完成的任务对我来说,任何帮助,好吗?
                              在此先感谢

                              Seems a mission impossible to me, any help, please? Thanks in advance

                              推荐答案

                              最后,这就是我所做的,它的工作原理很细的:

                              At last that's what I did, it works very fine:

                              // create array of ancestors' ID from current page
                              function path($page = 0) {
                                  global $database_connApp, $connApp;
                                  // let's create arrays
                                  do {
                                      mysql_select_db($database_connApp, $connApp);
                                      $query_rsPage = "SELECT pages.pag_id FROM pages WHERE pages.pag_id = " . $page;
                                      $rsPage = mysql_query($query_rsPage, $connApp) or die(mysql_error());
                                      $row_rsPage = mysql_fetch_assoc($rsPage);
                                      $bid[] = $row_rsPage['pag_id'];
                                      $page = $row_rsPage['pag_parent'];
                                  } while ($page > 0);
                              
                                  // move to the last array index
                                  end($bid);
                                  $output = $bid;
                                  return $output;
                              }
                              


                              // create the menu
                              function fmenu($parent, $array, $path) {
                                  $has_children = false;
                                  foreach($array as $key => $value) {
                                      if (in_array($value['parent'], $path)) {
                                          if ($value['parent'] == $parent) {
                                              if ($has_children === false && $parent) {
                                                  $has_children = true;
                                                  echo '<ul>' ."\n";
                                              }
                                              $active = ($_GET['iData'] == $value['id']) ? ' class="active"' : '';
                                              echo '<li' . $active . '>' . "\n";
                                                  echo '<a href="../pagine/' . $value['id'] . '/' . slugify($value['title']) . '.htm">' . html($value['title']) . '</a>' . " \n";
                                              echo "\n";
                                                  fmenu($key, $array, $path);
                                              echo "</li>\n";
                                          }
                                      }
                                  }
                                  if ($has_children === true && $parent) echo "</ul>\n";
                              }
                              


                              echo fmenu(0, $nested, path($row_rsEdit['pag_id']));
                              

                              这篇关于PHP:使用递归函数嵌套的菜单中,展开只有一些节点(不是所有的树)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆