SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Spaces to display levels in dir structure list ?

    Good day to you all,

    working on listing a directory the way I want, I'm trying that for each level of file there is a number of spaces in front of the file and/or folder of that level. EX :

    Folder 1
    --Folder 2
    --Image 1
    --Image 2
    --Image 3
    Image 1
    Image 2

    How can I add that spacing on each level ?

    Here is my code :


    PHP Code:


    <?php

    $directory 
    "Art/";
    function 
    dirList ($directory)
    {

        
    //create 2 arrays - one for folders and one for files
       
    $folders = array();
       
    $files = array();

        
    // create a handler for the directory
        
    $handler opendir($directory);

        
    // keep going until all files in directory have been read
    while (false !== ($file readdir($handler))) {  

            
    // if $file isn't this directory or its parent,
            // add it to the results array
            
    if ($file != '.' && $file != '..')
            
            
    // If file is directory, mark it in bold.
           
    if(is_dir($directory.$file)) { 
            
    array_push($folders,$file);
            
            
    // Else not styled
            
    }else{
            
    array_push($files,$file);
            
        }
        }


        
    // tidy up: close the handler
        
    closedir($handler);

        foreach(
    $folders as $folder) {
          echo 
    "<strong>".$folder."</strong>  <a href=\"javascript:show('".$folder."');\">Show</a>- <a href=\"javascript:hide('".$folder."');\">Hide</a><br />";
            
    echo 
    "<div id=\"".$folder."\">";
    dirList($directory.$folder.'/');

    echo 
    "<br/><br/></div>";
        
        
        }

        foreach(
    $files as $file) {
          echo 
    $file."<br />";
        }
        

    }

    dirList($directory);

    ?>

    Thanks !

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Small hint. Do it semantically, it's a list of files and folders so use HTML lists. They auto-embed too.

    If you don't like the bullets, use CSS (list-style-type: none) to remove them.
    PHP Code:
    <?php
    function dirList ($directory){
        
    $folders = array();
        
    $files = array();
        
    $handler opendir($directory);
        while (
    false !== ($file readdir($handler))) {  
            if (
    $file != '.' && $file != '..'){
                if(
    is_dir($directory '/' $file)) { 
                    
    $folders[] = $file;
                }else{
                    
    $files[] = $file;
                }
            }
        }
        
    closedir($handler);
        foreach(
    $folders as $folder) {
            
    PrintF('<li><strong>%1$s</strong> <a href="javascript:show(\'folder_%1$s\');">Show</a> - <a href="javascript:hide(\'folder_%1$s\');">Hide</a>'$folder);
            
    PrintF('<ul id="folder_%1$s">'$folder);
            
    dirList($directory '/' $folder);
            echo 
    '</ul>';
        }
        foreach(
    $files as $file) {
            
    PrintF('<li>%s</li>'$file);
        }
    }
    dirList('Art');
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Small hint. Do it semantically, it's a list of files and folders so use HTML lists. They auto-embed too.

    If you don't like the bullets, use CSS (list-style-type: none) to remove them.
    PHP Code:
    <?php
    function dirList ($directory){
        
    $folders = array();
        
    $files = array();
        
    $handler opendir($directory);
        while (
    false !== ($file readdir($handler))) {  
            if (
    $file != '.' && $file != '..'){
                if(
    is_dir($directory '/' $file)) { 
                    
    $folders[] = $file;
                }else{
                    
    $files[] = $file;
                }
            }
        }
        
    closedir($handler);
        foreach(
    $folders as $folder) {
            
    PrintF('<li><strong>%1$s</strong> <a href="javascript:show(\'folder_%1$s\');">Show</a> - <a href="javascript:hide(\'folder_%1$s\');">Hide</a>'$folder);
            
    PrintF('<ul id="folder_%1$s">'$folder);
            
    dirList($directory '/' $folder);
            echo 
    '</ul>';
        }
        foreach(
    $files as $file) {
            
    PrintF('<li>%s</li>'$file);
        }
    }
    dirList('Art');
    ?>

    Wow, impresive !
    Where did you got that : %1$s and %s ????

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ho, how can I take the space off, from the top of the files ?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, sorry, that was me forgetting to add the original <ul>s:
    PHP Code:
    <?php
    function IDFriendlyName($Directory){
        return 
    str_replace(array('/''.'), array('_''-'), $Directory);
    }
    function 
    dirList($directory){
        
    PrintF('<ul id="directory_%s">'IDFriendlyName($directory));
        
    _dirList($directory);
        echo 
    '</ul>';
    }
    function 
    _dirList ($directory){
        
    $folders = array();
        
    $files = array();
        
    $handler opendir($directory);
        while (
    false !== ($file readdir($handler))) {  
            if (
    $file != '.' && $file != '..'){
                if(
    is_dir($directory '/' $file)) { 
                    
    $folders[] = $file;
                }else{
                    
    $files[] = $file;
                }
            }
        }
        
    closedir($handler);
        foreach(
    $folders as $folder) {
            
    PrintF('<li><strong>%1$s</strong> <a href="javascript:show(\'folder_%2$s\');">Show</a> - <a href="javascript:hide(\'folder_%2$s\');">Hide</a>'$folderIDFriendlyName($directory '/' $folder));
            
    dirList($directory '/' $folder);
        }
        foreach(
    $files as $file) {
            
    PrintF('<li>%s</li>'$file);
        }
    }
    dirList('..');
    ?>
    Oh, and %s is a string marker for a variable in PrintF (and SprintF). %s is replaced with the first variable, the second %s by the second etc. If you want three markers to be replaced by, say, the 5th marker, you can use %5$s.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Ho, how can I take the space off, from the top of the files ?
    Haha! Ho!


    See sprintf().
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •