Page 1 of 1

List dir, levels, and css ?

Posted: Sat Mar 08, 2008 1:02 pm
by Peuplarchie
Good day to you all,
I have a piece of code which list all folder recurcivly within a specified folder and list it as a link.

Here it is :

Code: Select all

 
 
<?
 
// count # of image in current non-recursivly dir
  function CountDirq($qDir, $qRecurse)
  {
    $Countq = 0;
 
    $dq = dir($qDir);
 
    while ($Entry = $dq->Read())
    {
      if (!(($Entry == "..") || ($Entry == ".")))
      {
        if (Is_Dir($qDir . '/' . $Entry))
        {
          if ($qRecurse)
          {
            $Countq += CountDirq($qDir . '/' . $Entry, $qRecurse);
          }
        }
        else
        {
          $Countq++;
        }
      }
    }
    
    return $Countq;
  }
 
 
 
 
function getDirectorya( $pathq = '.', $level = 0 ){
 
//Locate file where variable of the folder icon
$myFileq = "http://test.com/Sidebar_icon/icon_change.txt";
$fhq = fopen($myFileq, 'r');
$outputtq = fgets($fhq);
$outputq = str_replace("../..", "http://http://test.com/V_0-1", $outputtq );
fclose($fhq);
 
//Strat reading directory and return folder and file
    $ignoreq = array( 'cgi-bin', '.', '..' );
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.
 
    $dhq = @opendir( $pathq );
    // Open the directory to the handle $dhq
    
    while( false !== ( $fileq = readdir( $dhq ) ) ){
    // Loop through the directory
    
        if( !in_array( $fileq, $ignoreq ) ){
        // Check that this file is not to be ignored
            
            $spacesq = str_repeat( 'f', ( $level * 4 ) );
            // Just to add spacing to the list, to better
            // show the directory tree.
            
            $restq = substr($fileq, 0, -4);
 
 
            if( is_dir( "$pathq/$fileq" ) ){
            // Its a directory, so we need to keep reading down...
            
echo '<a href="http://test.com/Art/images_public.php?folder='.$pathq.'/'.$fileq.'" class="blue0"> '.$spacesq.' <img src="'.$outputq.'" border="0" /> '.$fileq.'- '.CountDirq($pathq.'/'.$fileq, False).'</a>';
 
 
 
                getDirectorya( "$pathq/$fileq", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
 
           
            } else {
            
 
 
                // Just print out the filename
            
            }
        }
 
    }
    closedir( $dhq );
    // Close the directory handle
}
 
getDirectorya( "../Trips" );
// Get contents of the "files/includes" folder  
 
?>
 
 



From that code I need to make it do a real menu with in css style like the following peice of code :

Code: Select all

 
 
    <ul>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a>
            <ul>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a>
                    <ul>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
 
 



I there somebody that can guide me on this, I don'tknow how to implement the part of putting each level in a different <ul></ul>.

Thanks!
Take care !

Re: List dir, levels, and css ?

Posted: Sat Mar 08, 2008 3:28 pm
by Christopher
Just have each call to getDirectorya() output a <ul> before the loop and </ul> after the loop. Then wrap the link you output with <li> .... </li>.

Re: List dir, levels, and css ?

Posted: Sun Mar 09, 2008 6:28 pm
by Peuplarchie
Now I have this code but it only read one directory and return errors :

Code: Select all

 
 
<?
 
 
function dirlist($dir)
{       
 
 
        $dh = opendir($dir);
 
        // the trick is creating the appropriate list tags
        // before and after the loop
        echo "<ul>";
        while(($file = readdir($dh)) !== false)
        {
                if($file == "." || $file == "..")
                        continue;
                echo "<li>";
                if(is_file($file))
                {
 
                        echo "<a href=\"http://test.com/Art/images_public.php?folder='.$file.'\" class=\"blue0\">$file</a>";
                } else {
                        echo $file;
                        dirlist($file);
                echo "</li>";
                }
        }
        echo "</ul>";
 
}  
 
dirlist ("../../Art/Pictures/Pics/Pic-S");
?>