Skip to content
Advertisement

Foreach empty ul

I use these few lines of code to list folders and subfolders. It works pretty well but I don’t understand why I end up with :

<ul></ul>.

This is my PHP code :

<?php
    function listFolder($dir){
        $files = preg_grep('/^([^.])/', scandir($dir));

        // prevent empty ordered elements
        if (count($files) < 1)
            return;
            echo '<ul>';
                foreach($files as $file){
                       
                    if(is_dir($dir.'/'.$file)) {
                        echo '<li><a href="view.php?m='. $dir.'/'.$file.'" class="folder">'.$file;
                        echo '</a>';
                        listFolder($dir.'/'.$file);
                        echo '</li>';
                    }
                     
                }
                echo '</ul>'; 
            }
                
        echo '<ul class="menu">';
              listFolder('mails');
              echo '</ul>';
?>

and the HTML result

<ul class="menu">
   <ul>
      <li>
         <a href="view.php?m=mails/2020" class="folder">2020</a>
         <ul>
            <li>
               <a href="view.php?m=mails/2020/Janvier" class="folder">Janvier</a>
               <ul>
                  <li><a href="view.php?m=mails/2020/Janvier/semaine1" class="folder">semaine1</a></li>
                  <li><a href="view.php?m=mails/2020/Janvier/semaine2" class="folder">semaine2</a></li>
                  <li><a href="view.php?m=mails/2020/Janvier/semaine3" class="folder">semaine3</a></li>
               </ul>
            </li>
            <li>
               <a href="view.php?m=mails/2020/Fevrier" class="folder">Fevrier</a>
               <ul></ul>
            </li>
            <li>
               <a href="view.php?m=mails/2020/Avril" class="folder">Mars</a>
               <ul></ul>
            </li>
            <li>
               <a href="view.php?m=mails/2020/Avril" class="folder">Avril</a>
               <ul></ul>
            </li>
            <li>
               <a href="view.php?m=mails/2020/Mai" class="folder">Mai</a>
               <ul></ul>
            </li>
         </ul>
      </li>
   </ul>
</ul>

If anyone has any idea what the problem is…

Thank you very much for the help you can give me.

Advertisement

Answer

The location of ul is incorrect, see below

function listFolder($location)
{
    if (is_file($location)) {
        echo '<li> <a href="view.php?m=' . $location . '">' . basename($location) . '</a></li>';
        return;
    }

    if (!is_dir($location)) {
        return;
    }

    $dirs = scandir($location);
    foreach ($dirs as $dir) {
        if ($dir == '.' || $dir == '..') {
            continue;
        }
        $path = $location . '/' . $dir;
        if (is_file($path)) {
            echo '<li> <a href="view.php?m=' . $path . '">' . $dir . '</a></li>';
        } else if (is_dir($path)) {
            echo '<li><ul>';
                listFolder($path);
            echo '</ul></li>';
        }
    }
}

and to call

    echo '<ul class="menu">';
    listFolder('/your/path');
    echo '</ul>';

Edit: as per your comment if you only wants to list folders, I would go with glob method, make sure to provide path without a slash

function listFolder($dir, $parent){
    // $files = preg_grep('/^([^.])/', scandir($dir));
    $files = glob($dir.'/*', GLOB_ONLYDIR);
 
    if (count($files) < 1)
        return;

        if(!$parent)  echo '<ul>';
            foreach($files as $file){
                if ($file == '.' || $file == '..') {
                    continue;
                }
                   
                if(is_dir($file)) {
                    echo '<li><a href="view.php?m='.$file.'" class="folder">'.basename($file);
                    echo '</a>';
                    listFolder($file,false);
                    echo '</li>';
                }
                 
            }
            if(!$parent)  echo '</ul>'; 
}

which you need to call

echo '<ul class="menu">';
  listFolder('/your/location',true);
echo '</ul>';
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement