Generating an html list

Discussion in 'PHP' started by Peuplarchie, Jun 22, 2008.

  1. #1
    Good day to you all,
    The following code read recursively a directory and return the content in a html list.

    What I need to do is when the class "fly" is use the </li> tag should be after the the next level, like follow :

    
    
    
    MY WAY
    <ul class="sub">
        <li><a href="http://test.com/test_images.php" class="fly">Gatineau - Ottawa</a></li>
      <ul>
        <li><a href="http://test.com/test_images.php" class="">Aylmer</a></li>
        <li><a href="http://test.com/test_images.php" class="">Gatineau Park - Parc de la Gatineau</a></li>
        <li><a href="http://test.com/test_images.php" class="">Misc. - Divers</a></li>
        <li><a href="http://test.com//test_images.php" class="">Nat. Capital of Canada - Capital Nat. du Canada</a></li>
      </ul>
      
        <li><a href="http://test.com/test_images.php" class="fly">Gatineau - Ottawa</a></li>
      <ul>
        <li><a href="http://test.com/test_images.php" class="">Aylmer</a></li>
        <li><a href="http://test.com/test_images.php" class="">Gatineau Park - Parc de la Gatineau</a></li>
        <li><a href="http://test.com/test_images.php" class="">Misc. - Divers</a></li>
        <li><a href="http://test.com//test_images.php" class="">Nat. Capital of Canada - Capital Nat. du Canada</a></li>
      </ul>
    
    
    </ul>
    
    
    THE GOOD WAY
    <li><a href="#nogo11" class="fly">Zoom</a>
    <ul>
    <li><a href="#nogo12">35mm to 125mm</a></li>
    <li><a href="#nogo13">50mm to 250mm</a></li>
    <li><a href="#nogo14">125mm to 500mm</a></li>
    </ul>
    </li>
    
    
    
    PHP:

    Now here is my code :

    
    
    		echo "<ul class=\"sub\">\r";
    function dirlist($dir) 
    {     
        $ul = true;
        $dh = opendir($dir);
        $isLeaf = true;
        while (false !== ($file = readdir($dh)))
        {
            if ($file == '.' || $file == '..')
            {
                $subs = true;
                continue;
            }
            $filed = $file;
            $file = $dir . '/' . $file;
            $aHasDirs = array();
            $aHasDirs = glob(realpath($file) . '/*', GLOB_ONLYDIR);
            if (count($aHasDirs) > 0)
            {
                $class = 'fly';
                $ul = true;
            }
            else
            {
                $class = '';
                 $ul = false;
            }
            
            if (is_dir($file))
            {
                $isLeaf = false;
                echo '    <li><a href="http://test.com/test_images.php?folder=' . $file . '" class="' . $class . '">' . $filed . '</a></li>';
                echo "\n";
                
                if (!$isLeaf && $ul)
                {
                    echo "  <ul>\n"; 
                }
                dirlist($file);
                if (!$isLeaf && $ul)
                {
                    echo "  </ul>\n";
                }
            }
        }
    }  
    dirlist ("Pictures/Pics/Pic-S", false);
    
    echo "</ul>";
    
    
    PHP:

    Thanks and take care !
     
    Peuplarchie, Jun 22, 2008 IP
  2. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Resolved thamks !

    
    
    function dirlist($dir, $startDepth = 0)
    {
        static $depth;
        $spaceMulti = 4;
        $depth = empty($depth) ? $startDepth : $depth;
        $dh = opendir($dir);
        $isLeaf = false;
        $depth++;
    
        while (false !== ($file = @readdir($dh)))
        {
            if ($file == '.' || $file == '..')
            {
                continue;
            }
            $dirPath = $dir . '/' . $file;
            $aHasDirs = array();
            $aHasDirs = glob(realpath($dir . '/' . $file) . '/*', GLOB_ONLYDIR);
            if (count($aHasDirs) > 0)
            {
                $class = 'fly';
                $isLeaf = false;
            }
            else
            {
                $class = '';
                $isLeaf = true;
            }
    
            if (is_dir($dirPath))
            {
                echo "\n" . str_repeat(' ', $spaceMulti * $depth) . 
                    '<li><a href="http://test.comtest_images.php?folder=' . $dirPath . '" class="' . $class . '">' . $file . '</a>';
                if (!$isLeaf)
                {
                    $depth++;
                    echo "\n" . str_repeat(' ', $spaceMulti * $depth) . "<ul>";
                    dirlist($dirPath);
                    echo "\n" . str_repeat(' ', $spaceMulti * $depth) . "</ul>\r";
                    $depth--;        
                    echo "\n" . str_repeat(' ', $spaceMulti * $depth) . "</li>\r";
                }
                else
                {
                    echo "</li>\r";
                    dirlist($dirPath);
                }    
            }
        }
        fclose($dh);
        $depth--;
    }
    dirlist ("Pictures/Category/Pic-S", 0);
    
    [\php]
    PHP:
     
    Peuplarchie, Jun 22, 2008 IP
  3. Trusted Writer

    Trusted Writer Banned

    Messages:
    1,370
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Just a side note, there are small applications you can run on your computer to read your website's hard copy and create HTML formatted lists, just visit www.download.com to find them ;)
     
    Trusted Writer, Jun 22, 2008 IP