Folder and subfolder structure for navigation using PHP

Possible duplicate:
Help in the menu of the recursive PHP navigation menu

I am creating a navigation system that is only managed by the directory structure of directories and subfolders on the server.

for example: gallery

  • wedding
  • nature
    • birds
    • Cats
    • dogs
  • aviation
    • fighter jets
    • commercial
    • WWII

Now this code works for me until this moment, but I had problems with its formatting into the necessary unordered list and list items for my navigation system.

<?php
function readDirs($path){
  $dirHandle = opendir($path);
  while($item = readdir($dirHandle)) {
    $newPath = $path."/".$item;

if(is_dir($newPath) && $item != '.' && $item != '..') {
    echo "<a href='$newPath'>$item</a>";
    readDirs($newPath);
  }
 }
}

$path =  "./galleries";
readDirs($path);

?>

Here is the html formatting that I require as output from PHP:

    <ul>
        <li><a href="./galleries/wedding">wedding</a></li>
        <li><a href="./galleries/nature/">nature</a>
            <ul>
                <li><a href="./galleries/nature/birds/">birds</a></li>
                <li><a href="./galleries/nature/cats/">cats</a></li>
                <li><a href="./galleries/nature/dogs/">dogs</a></li>
            </ul>
        </li>
        <li><a href="./galleries/aviation/">aviation</a>
            <ul>
                <li><a href="./galleries/aviation/fighter jets/">fighter jets</a></li>
                <li><a href="./galleries/aviation/commercial/">commercial</a></li>
                <li><a href="./galleries/aviation/wwii/">wwii</a></li>
            </ul>
        </li>
    </ul>

Any help would be most appreciated in this matter.

Thanks in advance.

+1
source share

Source: https://habr.com/ru/post/1747109/


All Articles