简体   繁体   English

使用PHP文件夹创建HTML菜单

[英]Creating HTML menu with PHP folders

I'm programming a library and want to create a universal HTML menu with the project folders. 我正在编程一个库,并想用项目文件夹创建一个通用的HTML菜单。

I have this structure: 我有这个结构:

 root     menu folders   sub-menu folders

01_Home     01_aaa          01_aaa, 02_abb, 03_acc
            02_bbb          ...
            03_ccc          ...

$root = 'content';

$fileData = fillArrayWithFileNodes ( new DirectoryIterator ( $root ) );
function fillArrayWithFileNodes(DirectoryIterator $dir) {
    $data = array ();
    foreach ( $dir as $node ) {
        if ($node->isDir () && ! $node->isDot () && $node != 'includes' && $node != 'data') {

            $data [$node->getFilename ()] = fillArrayWithFileNodes ( new DirectoryIterator ( $node->getPathname () ) );
        }
    }
    return $data;
}
function transformName($file) {
    return str_replace ( '_', ' ', substr ( $file, strpos ( $file, '_' ) + 1 ) );
}

And I create the HTML menu here: 我在这里创建HTML菜单:

<?php
foreach ( array_keys ( $fileData ['01_Home'] ) as $option ) {
?>
<li><a href="content/01_Home/<?php echo $option; ?>"> <strong><?php echo transformName($option); ?></strong></a></li>
<?php
}
?>

Does anyone know how I can display the sub-menu folder names? 有谁知道我如何显示子菜单文件夹的名称?

Like what you did with fillArrayWithNodes, I would create a helper function that displays all the subnodes of a specific folder array, eg displayChildNodes($folder) 就像您对fillArrayWithNodes所做的一样,我将创建一个帮助器函数来显示特定文件夹数组的所有子节点,例如displayChildNodes($folder)

That function will take in an associative array from $fileData , eg $fileData['01_Home'] . 该函数将从$fileData中获取一个关联数组,例如$fileData['01_Home'] It will contain a similar for loop like fillArrayWithNodes, except this time we are checking if there is data, given that key. 它将包含一个类似于fillArrayWithNodes的for循环,除了这次我们检查是否存在数据(给定键)。 We check that by determining if it is an array (hence it would fit the if condition in fillArrayWithFileNodes), and if there are items in the array. 我们通过确定它是否是一个数组来检查它(因此它将适合fillArrayWithFileNodes中的if条件),以及该数组中是否有项目。

foreach($folderArray as $subNodeName => $subNodeNodes) {
    //1. Print out the folder name $subNodeName here. (like you did in the example)  
    //2. if is_array($subNodeNodes) && count($subNodeNodes) > 0, then call displayChildNodes($subNodeNodes) 
}

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

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