简体   繁体   English

PHP递归目录菜单

[英]PHP recursive directory menu

I think similar questions have been asked before, but I can't quite wrap my head around whether what I want to do is logicaly possible. 我认为之前已经提出了类似的问题,但是我无法完全理解我想要做的事情是否符合逻辑。

I currently use DDSmoothMenu on our intranet to list documents that we have for all staff to access. 我目前在内联网上使用DDSmoothMenu列出了我们所有员工都可以访问的文档。

Menu structure would be something like: 菜单结构类似于:

Documents -> Finance -> Forms -> File 1
                              -> File 2
                              -> File 3
                     -> Informational -> File 1
                                      -> File 2
          -> Insurance -> File 1
                       -> File 2

The basic structure of the menu is below: 菜单的基本结构如下:

<ul>
  <li><a href='#'>Sub Menu Name</a>
    <ul>
      <li><a href='#'>Menu Item</a></li>
      <li><a href='#'>Menu Item</a></li>
      <li><a href='#'>Menu Item</a></li>
      <li><a href='#'>Menu Item</a></li>
    </ul>
  </li>
</ul>

I think it would have to involve some kind of multidimensional array and a recursive directory iterator, but I would like to go through each folder and create the HTML layout as above. 我认为它必须涉及某种多维数组和递归目录迭代器,但我想浏览每个文件夹并创建如上所述的HTML布局。

I think it may be possible to do the opening tags, but not sure how to then do the closing tags once that directory is all listed. 我认为可能会执行开始标记,但不确定如果该目录全部列出后如何执行结束标记。

The easier way is use trees. 更简单的方法是使用树木。 I recommend Nested model You can check current and perv lvl of item. 我推荐嵌套模型你可以检查项目的当前和perv lvl。

A recursive solution could look something like: 递归解决方案可能类似于:

function createMenuHTML($dir){
    $html = "";
    if(is_dir($dir)){
        //Directory - add sub menu
        $html .= "<li><a href='#'>Sub Menu Name</a><ul>";
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                $html .= createMenuHTML($dir.$file);
            }
            closedir($dh);
        }
        $html .= "</ul>"
    }else{
        //File so  just add list item
        $html .= "<li><a href='#'>".basename($dir)."</a></li>"
    }
    return $html;
}

This is entirely untested but should hopefully help. 这完全未经测试,但应该有所帮助。

Ok, so here is what I ended up with thanks to Jim's example code: 好的,所以这就是我最后得到的结果,感谢Jim的示例代码:

    function createMenu($dir) {
    if(is_dir($dir)) {
        echo "<li><a href='#'>".basename($dir)."</a><ul>";
        foreach(glob("$dir/*") as $path) {
            createMenuHTML($path);
        }
        echo "</ul></li>";
    }
    else {
        $extension = pathinfo($dir);
        $extension = $extension['extension'];
        echo "<li><a href='$dir'>".basename($dir, ".".$extension)."</a></li>";
    }
}

createMenu("/public/Documents");

Works like an absolute charm for my DDSMoothMenu, and I can be as general or as granular as I want when using the function to create the menu. 对于我的DDSMoothMenu来说,它就像一个绝对的魅力,当我使用该功能创建菜单时,我可以像我想要的一般或细化。

I will mark this as the answer, but Jim gave me the best starting point possible code wise! 我会将此标记为答案,但Jim明智地给了我最好的起点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM